Search code examples
openlayersgeojsonepsg

Is there any automatic way to discover epsg code and proj4 to geojson file in openlayers?


I have a project to load some geojson files and then plot then in a openlayers map. I was doing fine with a sample file from Blumenau/Santa Catarina/Brazil (city/state/country) but when I uploaded new files from Brazil cities, they seen displaced in map.

Is there any way to automatic define proj4 params and epsg code when uploading files to my website? Actually, the page only upload files and define it by a new name, without alter it data. When I tried to upload Santo Andre/Sao Paulo/Brazil data, it is ploted in other state from Brazil, but with Santo Andre city format.

<link rel="stylesheet" href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.5.0/proj4.js"></script>

proj4.defs('EPSG:32722', '+proj=utm +zone=22 +south +ellps=WGS84 +datum=WGS84 +units=m +no_defs ');
ol.proj.proj4.register(proj4);

{% load static %}

var suscetibilidadeDeslizamento = new ol.layer.Vector({
                                      source: new ol.source.Vector({
                                        format: new ol.format.GeoJSON({dataProjection: 'EPSG:32722'}),
                                        url: '{% static deslizamento %}'
                                      }),
                                      style: style,
                                      opacity: 0.3
                                    });
var map = new ol.Map({
                          target: 'map',
                          layers: [

                            new ol.layer.Tile({
                                source: new ol.source.OSM()
                              }),
                                    suscetibilidadeDeslizamento

                          ],
                          view: new ol.View({
                            center: ol.proj.fromLonLat([-49, -27]),
                            zoom: 5
                          })
                        });

Here is Blumenau data ploted. Data: https://raw.githubusercontent.com/tiagoferneda/files/master/SuscetibilidadeInundacaoBlumenau.json Blumenau data

Here is Santo Andre data ploted, over Terra Boa city, far from correct location. Data: https://raw.githubusercontent.com/tiagoferneda/files/master/SuscetibilidadeInundacaoSantoAndre.json Santo Andre data


Solution

  • Try reading the CRS of your JSON before to create the ol.layer.Vector and then adding the corresponding proj4 params.

    You should take a look at this OpenLayers example where it loads proj4 params dynamically from EPSG.io