farmOS / farmOS-map

farmOS-map is an OpenLayers wrapper library designed for agricultural mapping needs. It can be used in any project that has similar requirements.
https://farmOS.github.io/farmOS-map
MIT License
40 stars 22 forks source link

Add GeoJSON layer from object, instead of URL #76

Closed mstenta closed 4 years ago

mstenta commented 4 years ago

The addLayer('geojson') (and associated internal addGeoJSONLayer()) method expects a GeoJSON URL to be provided. This adds a layer to the map, which then makes a separate request to the URL to load in GeoJSON data from it.

There are cases where you already have a GeoJSON string, and you just want to pass that in, without the need to load it from a URL. Currently, the only way to handle that is to convert the GeoJSON to WKT (which requires another library like Wicket) and then use addLayer('wkt').

This came of the discussion in: https://github.com/farmOS/farmOS-client/pull/350 - because Field Kit was using addLayer('geojson') to load the "all areas" geometries directly from farmOS, but with the move to OAuth2 this will no longer work (because there won't be a session cookie saved that gets passed to that endpoint automatically). So: Field Kit will need a way to add a GeoJSON layer to a map by passing a GeoJSON string to it, not a URL.

I think we could just add geojson as another parameter alongside url, and tweak the "required" logic a bit so that one or the other must be provided: https://github.com/farmOS/farmOS-map/blob/91cc8b687e462f56988b25ed579c43981dba24f1/src/instance/methods/layer.js#L204

(https://github.com/farmOS/farmOS-client/pull/350#issuecomment-632023703)

mstenta commented 4 years ago

Sketched this up in a PR: #77

This is not tested yet.

jgaehring commented 4 years ago

Awesome! Looks great!

jgaehring commented 4 years ago

Resolved by #77