I'm new to open layer 3 and i found an example of drawing a polygon :
I saw in the example that you create a vector layer and in that layer you give it a geoJSON as a source, the geoJSON has features which are something like this:
{
'type': 'Feature',
'geometry': {
'type': 'Polygon',
'coordinates': [[[-5e6, -1e6], [-4e6, 1e6], [-3e6, -1e6]]]
}
Now this works and i get a nice polygon, what i don't know is what are these coordinates? i mean what is a -5e6
?!, if i have a long and lat such as this:
[[36.301744, 50.010456], [36.302180, 50.019864], [36.301025, 50.021730], [36.293856, 50.016215], [66.293682, 56.009240], [66.301744, 56.010456]]
How can i convert it to these coordinates?,
Actually i did some search and i found some links :
open layers 3 how to draw a polygon programmatically?
But after using:
polygon.transform('EPSG:4326', 'EPSG:3857');
To transform
my coordinates (which i still don't know what i'm transforming to as i don't know what something like a -5e6
means), i still didn't get any result
Can someone please shine a light on what are these coordinates based on and how do they work ?
Thanks.
Update 01:
Here is the code i run which results in cannot read property length of undefined
inside geon/SimpleGeomerty
:
const GeographicalMap = ({ onClick }) => {
const myCoord = [[36.301744, 50.010456], [36.302180, 50.019864], [36.301025, 50.021730], [36.293856, 50.016215], [36.293682, 50.009240], [36.301744, 50.010456]]
const newCoord = transform(myCoord, 'EPSG:4326', 'EPSG:3857')
console.log('newCoord', newCoord)
const geojsonObject = {
'type': 'FeatureCollection',
'crs': {
'type': 'name',
'properties': {
'name': 'EPSG:4326'
}
},
'features': [
{
'type': 'Feature',
'geometry': {
'type': 'Polygon',
'coordinates': newCoord
}
},
]
};
var source = new VectorSource({
features: (new GeoJSON()).readFeatures(geojsonObject)
})
var layer = new VectorLayer({
source: source,
});
const layers = [layer]
return (
<Map
onClick={onClick}
layers={layers}
/>
)
}
What bothers me the most is the the console log after transformation shows this as a result:
[
null,
null,
[
36.301025,
50.02173
],
[
36.293856,
50.016215
],
[
36.293682,
50.00924
],
[
36.301744,
50.010456
]
]
Update 02:
As suggest by answer cabesuon
, i am now using the code below:
const myCoord = [[36.301744, 50.010456], [36.302180, 50.019864], [36.301025, 50.021730], [36.293856, 50.016215], [36.293682, 50.009240], [36.301744, 50.010456]]
const polygon = new Polygon(myCoord);
polygon.transform('EPSG:4326', 'EPSG:3857');
console.log('polygon', polygon)
const feature = new Feature({
geometry: polygon
});
const source = new VectorSource();
source.addFeature(feature);
const layer = new VectorLayer({
source: source,
});
But nothing is drawn on the map, after console logging the polygon, i can see a problem :
extent_: (4) [Infinity, Infinity, -Infinity, -Infinity]
ends_: (6) [0, 0, 0, 0, 0, 0]
I don't know why, but something seems to be incorrect in my code which results in incorrect extent_
and ends_