kartena / Proj4Leaflet

Smooth Proj4js integration with Leaflet.
http://kartena.github.io/Proj4Leaflet/
BSD 2-Clause "Simplified" License
590 stars 173 forks source link

How to determine the correct CRS when adding multiple geojson? #108

Closed MuellerMatthew closed 8 years ago

MuellerMatthew commented 8 years ago

Given that a GeoJSON layer in leaflet is an extension of the layer group and it is actually multiple individual layers each of which could have its own CRS, and given that it is possible to load multiple geojson files in to the same geojson layer, it seems premature to have all of the sub-layer's CRS set upon the initial layer creation and first geoJSON which is loaded. Instead shouldn't we assign each sublayer's CRS when the geojson file is actually loaded into the layer so that if we are loading multiple geojson's with different CRS into the same geojson layer, each sublayer is assigned the correct crs?

I think this change would be minor, Instead of having Proj4 change the 'initialization' to add the CRS upon initialization, it would assign the CRS in the 'addData' function so that it add the CRS to the sub layers whenever new data is added to the layer?

perliedman commented 8 years ago

Hi!

This sounds reasonable, but looking at the code, the CRS is already picked up in the addData method: https://github.com/kartena/Proj4Leaflet/blob/master/src/proj4leaflet.js#L291-302

What I think is the problem, is that Leaflet has a single coordsToLatLng option, which means that you really can't have more than one CRS in a single GeoJSON layer.

The solution is either to use one layer per CRS, or reproject the GeoJSON to the same CRS (preferably WGS84), something you could do before handing the GeoJSON to Leaflet with for example reproject.

MuellerMatthew commented 8 years ago

I'm sorry, I was actually looking at the refactored version for Leaflet 1.0.0-beta2 which no longer includes the addData method, and which only uses the initialization function: https://github.com/kartena/Proj4Leaflet/blob/leaflet-proj-refactor/src/proj4leaflet.js#L156-168

Right now the leaflet addData() function makes all of the sub-layers automatically inherit their parent layers options. The only way I can get multiple sub-layers with different CRS's in the same layer is by starting with separate GeoJSON layers and then looping through each layer calling addLayer() to move the sub-layers from one layer into another layer without inheriting/changing their options or the parents options.

perliedman commented 8 years ago

@MuellerMatthew ah, right. I realized that you were referring to leaflet-proj-refactor when I saw your PR. You were completely right that the implementation had problems since it didn't have the addData method; I've ported over the relevant commit to the 1.0 branch as well.