0

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?

6
  • Have you tried it? Was was the result?
    – TomazicM
    Apr 18 at 15:10
  • If I use pointToLayer to layer no matter what type of GeoJsonObject I have, all Point-related types of geometries will be turned from Markers to SVG elements. So it works fine, but I don't know how bad is the performance of this method Apr 18 at 21:19
  • Filtering has to be done at one point. You'll find out how efficient Leaflet filtering is on the basis of practical experience If you'll find it not efficient enough, you can then try your own, why bother with that before.
    – TomazicM
    Apr 19 at 9:54
  • Thanks! So it means that Leaflet will loop over an array of features and will make sure that a type of the feature will be either Point or MultiPoint before running the pointToLayer() function? Apr 20 at 1:06
  • Description from 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.
    – TomazicM
    Apr 20 at 7:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.