I have a project where I have Polygons drawn in a Openlayers map. These objects get turned into GeojSON using format.writeGeometry(plot.geometry.getGeometry())
. This is my first project using a PostGIS database in combination with OpenLayers and GeoJSON.
Then I send them to a NodeJS backend who then sends this data to a PostGIS database. 'INSERT INTO plots (user_id, geog) VALUES ($1, ST_GeomFromGeoJSON($2))'
. Where the geog
is the Geojson that I receive from the frontend.
The problem that I am facing is that the coordinates get distorted inside the Database. This is an example of how the coordinates are represented inside the database:
Here is the Geojson from the above example that is being inserted into the Database:
{
"type":"Polygon",
"coordinates":[
[
[
352847.7681741,
6591493.68325289
],
[
352853.14118403,
6591495.88702873
],
[
352865.86122686,
6591501.10405905
],
[
352866.13573636,
6591501.21660883
],
[
352866.14658836,
6591501.22104384
],
[
352856.32964058,
6591526.19583173
],
[
352854.06390643,
6591531.95998212
],
[
352853.92222987,
6591531.90191122
],
[
352766.24950317,
6591495.94284857
],
[
352766.18921573,
6591495.91814133
],
[
352778.56476466,
6591465.29940885
],
[
352847.7681741,
6591493.68325289
]
]
]
}
And the polygon that is being generated in the PostGIS database:
SRID=4326;POLYGON((47.76817410002695 -73.68325288966298,53.14118402998429 -75.88702872954309,65.86122686002636 -81.10405904985964,66.13573635998182 -81.21660882979631,66.14658836001763 -81.2210438400507,56.32964057999197 -73.80416826996952,54.06390642997576 -68.04001787956804,53.92222986998968 -68.09808878041804,-33.75049682997633 -75.94284856971353,-33.81078426999738 -75.91814132966101,-21.43523533997359 -45.29940885026008,47.76817410002695 -73.68325288966298))
format.writeGeometry(plot.geometry.getGeometry(), {dataProjection: 'EPSG:4326', 'featureProjection': 'EPSG:3857'})
orformat.writeGeometry(plot.geometry.getGeometry(), {dataProjection: 'EPSG:3857', 'featureProjection': 'EPSG:4326'})
Never able to sort out between input and output depending if write or read....writeGeometry
method input CRS isfeatureProjection
and output CRS isdataProjection
, see openlayers.org/en/latest/apidoc/…