1

I am using leaflet 1.0.0 to display the GeoJSON. The GeoJSON contains data for the whole bounding box but I want to display the feature in some location only.

Is it possible to clip the GeoJSON with another GeoJSON or something like that to display on the leaflet map?

2 Answers 2

2

Yes, it's possible, but it's not something that Leaflet can (or should) do.

Instead, research into other tools specialized in geoprocessing, like e.g. turf.js's bboxClip().

2
  • Not sure if turf's bboxclip can do but I do not want bounding box clip but rather a polygon clipping.
    – Biplov
    Nov 9, 2017 at 8:54
  • Looks like github.com/wandergis/turf-clip might work. I will give it a try.
    – Biplov
    Nov 9, 2017 at 9:15
1

As suggested by Biplov, wandergis/turf-clip is what you want. It's expensive though. This is what I ended up with

    var poly = turf.bboxPolygon(bbox);
    geojson.features=geojson.features
    .map((f)=>{
            return geojson_clip(poly,f).features
    })
    .filter((f)=>{
            return f != null && f.length > 0
    })
    .reduce((total,features)=>{
           return total=total.concat(features)
    })

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.