kartena / Proj4Leaflet

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

Defining projections based on WMS GetCapabilities information #64

Closed sellonen closed 10 years ago

sellonen commented 10 years ago

Hi,

When I send a GetCapabilities request to a wms server, I receive for each layer an EPSG code and bounding box. In openlayers they are enough to define a projection, it is really as simple as

var projection = ol.proj.configureProj4jsProjection({
  code: 'EPSG:21781',
  extent: [485869.5728, 76443.1884, 837076.5648, 299941.7864]
});

I have reasons to prefer Leaflet, but I'm a bit at loss why I need to define the projections so explicitly in proj4Leaflet. Does openlayers do a lot of magical mathematics to get from the epsg code and extent to the full crs specification?

I guess my main question is: can someone estimate how difficult it would be to make projection definitions so easy in leaflet that one could define the crs based on the response of GetCapabilities?

perliedman commented 10 years ago

For this functionality, OpenLayers uses older Proj4js version's functionality to dynamically load projection information from spatialreference.org. That is, the EPSG code in itself is not enough, but it will make a call to spatialreference to get the required information.

This functionality is not built in for newer Proj4js versions, and for good reason, since that asynchronous loading was quite poorly designed and was really outside the scope of Proj4js.

If you want to achieve the same thing, you can make the call yourself. An example of how it can be done can be seen here: https://github.com/perliedman/gis-nerd-tools/blob/master/src/projections.js (although that is part of an application, and I'm sure it can be done in fewer lines of code if you use for example jQuery or similar).

sellonen commented 10 years ago

I could not have wished for a better answer, thanks a lot! I certainly do not want to make an asynchronous call every time before the slowest part of my application starts loading, so I will try to find a way to cache the projections my customers need.

perliedman commented 10 years ago

Sounds like a good thing, if you know the EPSG codes in advance!