OpenLayers3: best way to display multiple WMS layers in trasparency - transparency

I'm writing a js script to add multiple WMS layers to a map using OpenLayers3
The code is something like this:
<script type="text/javascript">
var l1 = new ol.layer.Tile({
source: new ol.source.MapQuest({layer: 'sat'})
});
var l2 = new ol.layer.Image({
source: new ol.source.ImageWMS({
url: 'http://pubblicazioni.provincia.fi.it/geoserver/wms',
params: {'LAYERS': 'aree_protette1'},
})
})
var l3 = new ol.layer.Image({
source: new ol.source.ImageWMS({
url: 'http://pubblicazioni.provincia.fi.it/geoserver/wms',
params: {'LAYERS': 'aree_protette2'},
})
})
var map = new ol.Map({
target: 'map-dashboard',
view: new ol.View({
center: [-10997148, 4569099],
zoom: 4
})
});
map.addLayer(l1);
map.addLayer(l2);
map.addLayer(l3);
</script>
This way, the WMS layers are not all visible. The first ones are hidden by the last one.
Reading the API docs I found the ol.layer.TileWMS object so a possible solution would be to replace the ImageWMS objects:
var l2 = new ol.layer.Tile({
source: new ol.source.TileWMS({
url: 'http://pubblicazioni.provincia.fi.it/geoserver/wms',
params: {'LAYERS': 'aree_protette1'},
})
})
var l3 = new ol.layer.Tile({
source: new ol.source.TileWMS({
url: 'http://pubblicazioni.provincia.fi.it/geoserver/wms',
params: {'LAYERS': 'aree_protette2'},
})
})
I need to see all the layers visible and overlapped. I'm not sure that this is a working solution. Any further suggestion is welcome!

You want the two layers in two seperate ol3-layers, too?
if not then you could try (depending on the data)
var layers = new ol.layer.Tile({
source: new ol.source.TileWMS({
url: 'http://pubblicazioni.provincia.fi.it/geoserver/wms?',
params: {'LAYERS': 'aree_protette1, aree_protette2 '},
})
As two seperate layers, try to set the transparent and image (gif or png) parameter
'LAYERS': 'aree_protette1',
'FORMAT': 'image/png', //depending what the GetCapabilities says
'TRANSPARENT': 'true'
Edit:
Of course it depends on the data provided, if the transparency-parameter will be useful. If so you can set the opacity of the layer in ol3.

Related

GML3 features Openlayers not plotting

Latest Version of Openlayers 3.
var metSource = new ol.source.Vector({
url: "metobjectxml/2.xml",
format: new ol.format.GML3({
defaultDataProjection: 'EPSG:4326'
})
});
var metLayer = new ol.layer.Vector({
source: metSource
});
var mapView = new ol.View({
center: ol.proj.transform([-114.3035, 54.5800], 'EPSG:4326', 'EPSG:900913'),
zoom: 5,
projection: 'EPSG:900913'
});
var map = new ol.Map({
layers: [metLayer],
target: 'map',
view: mapView
});
```
Not plotting anything.
XML file: https://bpaste.net/show/56363886838f
Anyone know why?
This is not supported by OpenLayers. Only GML simple features profile is supported. What you have seems sensor web related.

How to use features from Geojson in openlayers3?

I am using openlayers3 and i want to bring my features from feature.json, it seems when my map is loded I can get the feature file from network as xhr request but i am not able to see my polygons on map. Here below is my code
function showMap() {
var vector = new ol.layer.Vector({
projection: 'EPSG:5650',
source: new ol.source.Vector({
format: new ol.format.GeoJSON(),
projection: 'EPSG:5650',
url: 'feature.json'
})
});
var map = new ol.Map({
target: 'tilemap',
controls: ol.control.defaults({
attributionOptions: /** #type {olx.control.AttributionOptions} */ ({
collapsible: false
})
}),
layers: [
new ol.layer.Image({
source: new ol.source.ImageWMS({
url: 'http://www.geodaten-mv.de/dienste/adv_dop',
crossOrigin: null,
projection: 'EPSG:5650',
params: {
VERSION: '1.3.0',
LAYERS: 'mv_dop',
FORMAT: 'image/jpeg',
CRS: 'EPSG:5650'
}
})
}),vector
],
view: new ol.View({
projection: 'EPSG:5650',
center: [33355494, 5983295],
zoom: 10
})
});
DisplayTilesServices.setMap(map);
}
I was using olderversion of openlayers and now instead of Using url in source, now I am using features and it works!! see the code below
$http.get('feature.geojson').then(function(res){
var format = new ol.format.GeoJSON();
source=new ol.source.Vector({
projection: 'EPSG:5650',
features:format.readFeatures(res.data)
});
var vectorLayer = new ol.layer.Vector({
source: source
});
map.addLayer(vectorLayer);
},function(){})

OL3 Color Icon in KML

Now I am trying to migrate Google Earth to OpenLayers 3 (version 3.11.1)
I use a KML file, while viewing with GE, the icon is colored but with OL3, the color is not displayed
Here is my code snippet to post this KML file:
var projection = ol.proj.get('EPSG:3857');
var raster = new ol.layer.Tile({
source: new ol.source.BingMaps({
imagerySet: 'Aerial',
key: 'Ak-dzM4wZjSqTlzveKz5u0d4IQ4bRzVI309GxmkgSVr1ewS6iPSrOvOKhA-CJlm3'
})
});
var vector = new ol.layer.Vector({
source: new ol.source.Vector({
url: '/UsersFiles/KMLTest.kml',
format: new ol.format.KML({ extractStyles: true })
})
});
var map = new ol.Map({
layers: [raster, vector],
target: document.getElementById('map4'),
view: new ol.View({
center: [0 ,0],
projection: projection,
zoom: 0
})
});
the result is on the image below:
Icon in Google Earth
Icon in OL3

How to set zoom and center automatically based on GeoJSON features

I load some features from .json file, and would like to automatically set view wuth zoom to see all loaded features. I user OpenLayers version 3.
Here's what I have:
var gjsonFile = new ol.layer.Vector({
source: new ol.source.GeoJSON({
url: 'map.json',
projection: 'EPSG:3857'
})
})
});
var map = new ol.Map({
view: new ol.View({
center: ol.proj.transform([-77.0087,38.8691], 'EPSG:4326', 'EPSG:3857'),
zoom: 12
}),
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
}),
gjsonFile
],
target: 'map1'
});
Vector sources have a #getExtent() method that gives you the extent of all features currently loaded in the source. To set the map's view to that extent as soon as the file is loaded, add the following code:
var source = gjsonFile.getSource();
var onChangeKey = source.on('change', function() {
if (source.getState() == 'ready') {
source.unByKey(onChangeKey);
map.getView().fitExtent(source.getExtent(), map.getSize());
}
});

OpenLayers setting position of overlay on a static image

I have created a map with OpenLayers 3 using static image. It uses fake projection so the map can use to properly display the layer as it's measured in pixels. This is the code:
var pixelProjection = new ol.proj.Projection({
code: 'pixel',
units: 'pixels',
extent: [0, 0, 1389, 1070]
});
var map = new ol.Map({
layers: [
new ol.layer.Image({
source: new ol.source.ImageStatic({
url: 'http://s25.postimg.org/4o15oqbmn/jdgf.jpg',
imageSize: [1389, 1070],
projection: pixelProjection,
imageExtent: pixelProjection.getExtent()
})
})
],
target: 'map',
view: new ol.View2D({
projection: pixelProjection,
center: ol.extent.getCenter(pixelProjection.getExtent()),
zoom: 2
})
});
I was trying to add marker overlays to add some more interactions however I'm struggling to specify position and the markers is outside the map rather then inside where I want to place it.
var marker = new ol.Overlay({
position: [200, 200],
element: document.getElementById('marker'),
stopEvent: false
});
I'm very novice to this so if anyone have an idea how to set position correctly I will be thankful.
Using this OpenLayers 3 map as an example, I was able to add a marker to the center of your static image with the following:
var iconFeature = new ol.Feature({
geometry: new ol.geom.Point([700, 700])
});
var iconStyle = new ol.style.Style({
image: new ol.style.Icon({
src: 'http://ol3js.org/en/master/examples/data/icon.png'
})
});
iconFeature.setStyle(iconStyle);
var vectorSource = new ol.source.Vector({
features: [iconFeature]
});
var vectorLayer = new ol.layer.Vector({
source: vectorSource
});

Resources