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.

Layer Type defaults to Polygon even when drawing Rectangles

See original GitHub issue

The draw:created event’s e.layer, when converted to geoJSON says that geometry.type is a Polygon, even after drawing a Rectangle. Therefore, in Edit mode - the Rectangles are not movable/vertices can be pulled to warp the shape. Any idea what might be causing this? It’s using the default L.Draw.Rectangle and options.rectangle for the nodeHandlers (as it does out of the box), so not sure why the created event says its a Polygon.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ghostcommented, Feb 10, 2019

I use a similar trick to @Sandbird’s that I found somewhere else, where I define the geojson’s onEachFeature to something like:

L.geoJSON(geojsonFeature, {
  onEachFeature: function (feature, layer) {
    // check the custom 'rectangle' property
    if (layer instanceof L.Polygon && layer.feature.properties.rectangle) {
      layer = L.rectangle(layer.getBounds());
    }

    layer.addTo(addTo)}
})

Which works fine at making the rectangle be edited as a rectangle instead of a polygon.

Unfortunately, this trick seems to prevent click event handlers added to rectangles to work (they do work for other shapes where no trick was applied), as shown in this fiddle (open, click the big rectangle in the middle, ensure nothing happens). If, however, the conversion to L.rectangle() is commented out (as in this other fiddle), the event handler is properly called (open, click the big rectangle in the middle, close annoying alert).

Oddly enough, it works once again (even with the trick enabled) if the click handling is defined inside onEachFeature, as this final fiddle shows.

1reaction
Sandbirdcommented, Oct 4, 2018

There is a way to do it. Treat is as a MultiPolygon. A typical Polygon’s coordinates has 2 brackets. a MultiPolygon has 3, (so you can add ‘cuts’ into the polygon): poly

So all you have to do is add an extra [ ] to your geojson coordinates and name it type: MultiPolygon. I am creating my geojson object via PHP/MySQL so this is how i create a layer to place the objects inside: bef Looking at the type, i decide which of the 3 types to create in the Layer. (i dont use markers so Points is used for circles…i just need to send the radius as well) And here is the final result in edit mode: results

Read more comments on GitHub >

github_iconTop Results From Across the Web

Create rectangular features
The Rectangle tool creates planar rectangular or square single-part features ... clip existing polygons on the same layer if they overlap your new...
Read more >
Сreating polygons with straight lines and right angles in QGIS
The Orthogonal Digitizing tool is still drawing vertexes exactly where I point. My CADTools version is 0.5.9 and QGIS version 1.8. I've tried...
Read more >
how to prevent drawing polygons on an existing shapes layer
I have recently started working on web mapping with leaflet js 1.0 . I started working with leaflet-draw but I don't find a...
Read more >
How to create and modify live shapes in Illustrator
You can now draw circles, ellipses, polygons, and lines as Live ... Live shapes are displayed as a different object type in the...
Read more >
Photoshop Shapes And Shape Layers Essentials
By default, the Rectangle Tool is the one that's visible in the Tools panel, ... Photoshop actually lets us draw three very different...
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