1

I have code in OpenLayers, which allows you to draw geometries on a layer and then download the file in GeoJSON format.

Here's the code:

function downloadGeoJSON(content, fileName, contentType) { var a = document.createElement("a"); var file = new Blob([content], { type: contentType }); a.href = URL.createObjectURL(file); a.download = fileName; a.click(); } var json = new ol.format.GeoJSON().writeFeatures(vector_layer.getSource().getFeatures(), { dataProjection: 'EPSG:4326', featureProjection: 'EPSG:3857' }); downloadGeoJSON(json, 'my_layer.geojson', 'text/plain'); 

Note: in the file, the features are saved with geographic CRS EPSG:4326, units in degree.

Then I can upload GeoJSON file. When the file was created in the OpenLayers application, the download works correctly. However, this file may have been created in QGIS, in EPSG:31983 with the unit in meters. In this case, how should I resolve it?

I need to convert to EPSG:4326 units in degrees, but I can't.

This is an example of geometry that is in file EPSG:31983

"geometry": { "type": "Point", "coordinates": [ 342273.613602064666338, 7384707.507063377648592 ] }

This is an example of geometry that is in file 4326

"type":"Point","coordinates":[-46.526534460937505,-23.653072477438627]}

Here is the upload code:

myFile.files[0].text().then(function (text) { vector_source.addFeatures( new ol.format.GeoJSON().readFeatures(text, { dataProjection: 'EPSG:4326', featureProjection: 'EPSG:3857' }) }); 

I need to convert the EPSG:31983 file for the upload to work.

6
  • 2
    Geojson should always be in 4326 Commented Apr 3, 2024 at 17:00
  • @IanTurton Hello, I have a layer in software QGis, in SIRGAS 2000 and exported it to geojson, so it ended up with 31983. And I need to import this geojson into Openlayers and I'm not able to Commented Apr 3, 2024 at 17:16
  • 1
    You can select the CRS when you export GeoJSON from QGIS. Select "Default CRS: EPSG:4326 - WGS 84". Commented Apr 3, 2024 at 19:06
  • @user30184 It is an Openlayers application, in which the user chooses the file to upload. And this file, most of the time, will come in SIRGAS 2000. So, that's why I would like to convert, because it is something external, and in general, our layers are almost all in SIRGAS 2000. Commented Apr 3, 2024 at 19:30
  • @IanTurton GeoJSON is not EPSG:4326 it's CRS:84 Commented Apr 4, 2024 at 13:57

1 Answer 1

2

To convert CRS of GeojSON features from EPSG:31983 to EPSG:3857 when reading them with .readFeatures method, you first have do define EPSG:31983 projection to be used during conversion (see example https://openlayers.org/en/latest/examples/reprojection.html).

Definition string for EPSG:31983 projection is available at https://epsg.io/31983 in section proj4js.

Since it seems there is a bug in OpenLayers when transforming coordinates from EPSG:31983 when reading GeoJSON with .readFeatures method, coordinates can be transformed using proj4 transformation. To iterate over all GeoJSON coordinates turf.js library turf.coordEach method can be used.

Relevant part of the code could then look something like this:

proj4.defs('EPSG:31983","+proj=utm +zone=23 +south +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs +type=crs'); ol.proj.proj4.register(proj4); myFile.files[0].text().then(function (text) { var geoJSON = JSON.parse(text); turf.coordEach(geoJSON, function (currentCoord) { var newCoord = proj4('EPSG:31983','EPSG:3857', currentCoord); currentCoord[0] = newCoord[0]; currentCoord[1] = newCoord[1]; }); vector_source.addFeatures( new ol.format.GeoJSON().readFeatures(geoJSON) ) }); 

Coordinate [342273.613602064666338, 7384707.507063377648592] then ends here:

enter image description here

6
  • Thanks. However, it did not show the point in the right place. In the file with the projection 31983 it is in meters format "geometry": { "type": "Point", "coordinates": [ 342273.613602064666338, 7384707.507063377648592 ] } and when I generate the file with projection 4326, it is in degrees: "type":"Point","coordinates":[-46.526534460937505,-23.653072477438627]}. How can I do this conversion? Commented Apr 3, 2024 at 18:48
  • 1
    Code in answer should work, but there seems to be a bug in OpenLayers when transforming this kind of projection. I'll post solution that works. Commented Apr 3, 2024 at 20:18
  • See modified answer. Commented Apr 3, 2024 at 20:53
  • Hello, I believe I have to check the code, because now I got another error, right when I choose the file and the code isn't even executed: Uncaught (in promise) EPSG:31983 Promise.then (asynchronous) . Line: myFile.files[0].text().then(function (text) { Commented Apr 4, 2024 at 12:55
  • Do you have proj4.js and turf.js libraries linked? Commented Apr 4, 2024 at 13:22

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.