question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

toGeoJSON of L.Circle doesn't have the radius of the circle

See original GitHub issue

circle.toGeoJSON() print something like

{
    "type": "Feature",
    "properties": {},
    "geometry": { 
        "type": "Point",
        "coordinates": [30.5,50.5]
     }
}

There is no Radius in the geometry so if I save the geometry I can’t show the circle in my map again just with the geometry. 😢

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:11
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

11reactions
basisbitcommented, Oct 13, 2017

this issue is open already for almost 2.5 years. It is ridiculous to not solve the issue because of some minor technical difficulties. Just returning a GeoJson-point for L.Circle.toGeoJSON definitely is a bug and a huge loss of information. A practical solution which is closer to the desired result would be to return a polygon in GeoJson instead. A polygon also would allow for point in area calculation, merging of geometry area,… without requiring the developers to add extra cases for L.Circle everywhere. Whatever you do, the current behavior is about as bad of a solution as it can get.

2reactions
dvsoukupcommented, Apr 19, 2016

I know this is somewhat of an outdated topic… but I ran into the same problem. Sounds like I had the same problem as @jbcoder. Leaflet is storing feature type as a “point” and not a “circle” when it comes to GeoJSON. From the thread here, it would seem that this is correct, though the only difference is that circles contain a radius property.

I can easily get this to work with single features, but it posed a problem with feature collections.

So what you can do is implement the first code block that @MarkAPhillips suggests in a couple posts up. That is to capture the radius property and add it to the feature inside the draw event. Ideally you could save this back to your DB, along with a “radius” property. Individual point markers should NOT have a radius property.

Then modify the actual leaflet.js to allow the handling of circle drawing when calling on L.geoJson (and passing a feature collection rather than just a single feature…). In the switch clause for the point, you can check if the radius property exists. If so, instead of returning a pointToLayer for the point, return a L.Circle, passing the radius. Seems to work fine for me.

L.extend(L.GeoJSON, {
    geometryToLayer: function (geojson, pointToLayer, coordsToLatLng, vectorOptions) {
        var geometry = geojson.type === 'Feature' ? geojson.geometry : geojson,
            coords = geometry.coordinates,
            layers = [],
            latlng, latlngs, i, len;

        coordsToLatLng = coordsToLatLng || this.coordsToLatLng;

        switch (geometry.type) {
        case 'Point':
            latlng = coordsToLatLng(coords);
//Add these two lines here...
                if (geojson.properties.radius)
                return new L.Circle(latlng, geojson.properties.radius);
            return pointToLayer ? pointToLayer(geojson, latlng) : new L.Marker(latlng);

Until this is resolved I can’t actually use the CDN to pull in this library, and have to use my own version locally.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Handle Circles in GeoJSON - Medium
You can extract the radius of the circle layer and store it inside the custom properties of a GeoJSON. Using Leaflet, it could...
Read more >
geojson circles, supported or not? - Stack Overflow
When I look up the specs of GeoJson I see that circles are supported. They aren't. Seems you managed to find some fake...
Read more >
L.Circle - WRLD3D
Instantiates a circle object given a geographical point, and an options object which contains the circle radius. L.circle(<LatLng> latlng, <Number> radius, ...
Read more >
How to get the exact Circle that user has drawn using Leaflet ...
map.on(L. ... Draw.Event.CREATED, function (event) { var layer = event.layer; console.log(layer.toGeoJSON()); drawnItems.addLayer(layer); });.
Read more >
L.Circle
setRadius ( <Number> radius ), this, Sets the radius of a circle. Units are in meters. toGeoJSON(), Object, Returns a GeoJSON representation of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found