Ambiguity around mapping between GeoJSON FeatureCollection and Leaflet's Layers and LayerGroups - leaflet

The Leaflet documentation says that its GeoJSON object inherits from its FeatureGroup object which inherits from its LayerGroup object which inherits from its Layer object. It also says that the GeoJSON object can take as input a GeoJSON Feature or FeatureCollection. If I create a Leaflet GeoJSON object with a GeoJSON FeatureCollection, is each GeoJSON Feature in the FeatureCollection a Leaflet LayerGroup or is each GeoJSON feature a Layer in the LayerGroup? So then if I only had a GeoJSON Feature and not a FeatureCollection, is that just a Layer and not a LayerGroup? I'm a bit confused on the mapping between the two models.

When you do:
L.geoJSON(whatever)
...you get a Leaflet "GeoJSON" Layer Group, which indeed inherits from Leaflet Feature Group.
Then what is contained in that group indeed depends on what is "whatever":
a single GeoJSON Feature with simple geometry (e.g. no multipoint) => a single Leaflet Layer
an array of such simple Features, or a Feature Collection of such simple Features => as many Leaflet Layers (non group)
for each item which is not a simple Feature (e.g. a nested Feature Collection, a Feature with Geometry Collection, or a complex geometry like multipoint), you will get a nested Leaflet GeoJSON Layer Group, which contains the corresponding child Layers (or nested groups again if necessary)

Related

Mapbox gl draw add name to polygon

I am playing around with mapbox one of the things im trying to figure out is how to set a name on a polygon that i have just created. I have a feature collection and for each feature i have a name but i cant figure out how to display the name on the polygon.
In general, if you'd like to display GeoJSON features along with labels in your Mapbox GL JS map, you will need to add two layers to your map referencing the GeoJSON source. One layer will be of type fill to display the polygons themselves, and the other will be of type symbol to display the text for the labels.
This example demonstrates how to add a symbol layer with text-fields containing the names of the Point features in the GeoJSON source. If you have the coordinates for each polygon where you'd like the name to be displayed, you can follow this example to add the names after adding your polygons with a fill layer.
Otherwise, you will need to compute the coordinates of the centroid of each polygon where you would like the names to be added. For this, you can use Turf's centroid method on each polygon in your FeatureCollection to generate said coordinates and add the symbol layer representing each feature's name with the resulting coordinates.

How to add pattern to GeoJson shape on a React-Leaflet map?

I am using React-Leaflet to display a Leaflet.js map. I use React-Leaflet GeoJSON (source here) to show a layer of polygons on the map.
Although I can vary the opacity of the fill (fillOpacity) of the polygon, I cannot find an option to fill the polygon with a pattern (e.g. checkers, stripes, etc.)
I have found a project (Leaflet.pattern) that supports adding fill patterns to Leaflet.js shapes, but it has not been adapted for use with React-Leaflet.
So my question is: How can I add patterns to React-Leaflet shapes, or adapt Leaflet.pattern for use with React-Leaflet? (for the latter, instructions on how to create custom React-Leaflet components are here, but I don't know how to do it for this case - it seems more complex than usual)
Take a look at the section in the docs about extending react-leaflet
If you are using react-leaflet v1 you can directly extend react-leaflet's GeoJSON class. If you are using v2 you'll need to extend the Path class instead. To understand why that's necessary take a look at this issue.
Once you've figured out which class to extend it's likely that most of your changes will need to happen in the createLeafletElementfunction and maybe the updateLeafletElement function.

Multiple geometries from one Geojson source without multiple layers

I'm following this example:
https://www.mapbox.com/mapbox-gl-js/example/multiple-geometries/
For each feature / shape on the map, they draw a new layer. However, I want to make "set" of features per Geojson I'm rendering on the map to be its own layer. Is it possible to group them somehow?
In Mapbox GL, each layer is associated with exactly one "shape" (circle, line, symbol, fill, raster, or fill-extrusion). It is not possible to render multiple "shapes" per layer.
You can control which features are rendered in a particular layer using filters.
Does that provide any more clarity? Can you describe specifically what you're trying to do?

MapBox GL JS Query Feature Geometry Units

In MapBox GL JS, when I call queryRenderedFeatures or querySourceFeatures, it returns an array of features with geometries. Using a vector tile source, the feature geometry objects have coordinates like (-154.34, 80.99) for a location near the lat/lon (-77.37, 38.98). In what projection are the coordinates? And is there a way to convert them to WGS84?
It turns out, this is a MapBox bug. I added a bug report here: https://github.com/mapbox/mapbox-gl-js/issues/4011.
The geometry objects are supposed to have GeoJson lat/lons.

Leaflet — point in which polygon? Rtree?

Does leaflet provide a method for determining which, if any, rendered point is within a polygon? Not by iterating over a bunch of shapes and checking bounds, but by using an rtree index, perhaps?
See the Leaflet plugins page, on which you'll find leaflet point-in-polygon and Leaflet.LayerIndex.