In my code I can expect any GeoJsonObject type (Point | MultiPoint | LineString | MultiLineString | MultiPolygon | GeometryCollection | Feature | FeautureCollection). I want to check if the type is Point or MultiPoint, then use the pointToLayer function to prevent a default behavior of drawing Markers and replace it with an SVG element. For example:
export function convertPointToSvgMarker(geojson: GeoJsonObject) {
return L.geoJSON(geojson, {
pointToLayer: function (feature, latlng) {
return L.circleMarker(latlng, {
fillOpacity: 0.5
})
}
})
}
But before using pointToLayer, I have to check if GeoJsonObject is Point or MultiPoint because this function is supposed to work with these specific types. The problem is it's hard to find out if GeoJsonObject has a Point, since, for example, in the case of FeatureCollection, Point can be nested somewhere deep in the object:
For example, FeatureCollection has features and GeometryCollection is one of its features, GeometryCollection in its turn has an array of geometries, and Point is somewhere inside this array...
so I have to go recursively and find a Point | MultiPoint in various GeoJsonObject structures. My question is what if I would use pointToLayer function no matter what type of GeoJsonObject I have? Can I just use it all the time trying to convert Marker to SVG element? It seems working, but I am curious what work pointToLayer does inside and how it can affect performance if I use it everywhere without checking if GeoJsonObject actually has a Point somewhere inside?
pointToLayer
Leaflet docs: A Function defining how GeoJSON points spawn Leaflet layers. It is internally called when data is added, passing the GeoJSON point feature and its LatLng.