Saturday, 4 August 2018

openlayers - GeoJSON - how to style features of GeometryCollection


I have a GeoJSON file with a feature as follows:


{

"type": "Feature",
"properties": {
"Start Date": "01-09-2015",
"End Date": "01-09-2015"
},
"geometry": {
"type": "GeometryCollection",
"geometries": [
{
"type": "Point",

"name": "test",
"coordinates": [74.86083984375, -52.94201777829492]
},
{
"type": "LineString",
"coordinates": [[74.86083984375, -52.94201777829492], [75.5419921875, -53.10721669189339]]
},
{
"type": "Point",
"coordinates": [75.5419921875, -53.10721669189339]

}
]
}
},

As you can see it consists of 2 points with a line joining the two points.


I want to style the 2 points with different colors but am stumped as to how to do that. At the moment I have the following ....


        var styleEnd = [
new ol.style.Style({
image: new ol.style.Circle({

radius: 7,
fill: new ol.style.Fill({
color: '#FF4500',
opacity: 0.8
})
})
})
];

var styles = {

'Point': [new ol.style.Style({
image: new ol.style.Circle({
radius: 7,
fill: new ol.style.Fill({
color: 'black'
})
})
})],
'LineString': [new ol.style.Style({
stroke: new ol.style.Stroke({

color: 'green',
width: 1
})
})],
'MultiLineString': [new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'green',
width: 1
})
})],

'MultiPolygon': [new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'yellow',
width: 1
}),
fill: new ol.style.Fill({
color: 'rgba(255, 255, 0, 0.1)'
})
})],
'Polygon': [new ol.style.Style({

stroke: new ol.style.Stroke({
color: 'blue',
lineDash: [4],
width: 3
}),
fill: new ol.style.Fill({
color: 'rgba(0, 0, 255, 0.1)'
})
})],
'GeometryCollection': [new ol.style.Style({

stroke: new ol.style.Stroke({
color: 'magenta',
width: 2
}),
fill: new ol.style.Fill({
color: 'magenta'
}),
image: new ol.style.Circle({
radius: 10,
fill: null,

stroke: new ol.style.Stroke({
color: 'magenta'
})
})
})],
'Circle': [new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'red',
width: 2
}),

fill: new ol.style.Fill({
color: 'rgba(255,0,0,0.2)'
})
})]
};

var styleFunction = function (feature, resolution) {
if (feature.getGeometryName() === 'test') {
return styleEnd;
} else {

return styles[feature.getGeometry().getType()];
}
};

var vectorLayer = new ol.layer.Vector({
source: new ol.source.Vector({
url: 'map.geojson',
format: new ol.format.GeoJSON()
}),
style: styleFunction

});

Answer



I solved my problem with the following style function:


var styleFunction = function (feature, resolution) {
var geometries = feature.getGeometry().getGeometries();
var start = geometries[0];
var line = geometries[1];
var end = geometries[2];

var startStyle = new ol.style.Style({

geometry: start,
image: new ol.style.Circle({
radius: 7,
fill: new ol.style.Fill({
color: '#32CD32'
})
})
});

var endStyle = new ol.style.Style({

geometry: end,
image: new ol.style.Circle({
radius: 7,
fill: new ol.style.Fill({
color: '#FF4500',
opacity: 0.8
})
})
});


var lineStyle = new ol.style.Style({
geometry: line,
fill: new ol.style.Fill({
color: 'rgba(255, 255, 255, 0.2)',
weight: 4
}),
stroke: new ol.style.Stroke({
color: '#808080',
width: 4
})

});

return [startStyle, lineStyle, endStyle];

};

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...