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.

Rectangle has only 3 LatLngs if first and last latlng are equal

See original GitHub issue

The logic of _convertLatLngs in L.Polygon removes the last latlng if it is the same as the first latlng. But a rectangle calls this too and then it results in having only 3 instead of 4 latlngs. --> Is not longer a valid Rectangle.

Both times the rectangle has only 3 latlngs left:

r = L.rectangle([[0,0],[0,0]])
console.log(r.getLatLngs())
r.setLatLngs([[1,1],[0,0],[0,0],[1,1]])
console.log(r.getLatLngs())

The problem code: https://github.com/Leaflet/Leaflet/blob/620bc3e9c5d282090f429ac21d28d00fbffdb3db/src/layer/vector/Polygon.js#L99-L108

I think this can be easily fixed when this code is added to L.Rectangle:

 _convertLatLngs: function (latlngs) { 
 	var result = Polyline.prototype._convertLatLngs.call(this, latlngs), 
 	    len = result.length; 
  
 	// remove last point if it equals first one 
 	if (len >= 4 && result[0] instanceof LatLng && result[0].equals(result[len - 1])) { 
 		result.pop(); 
 	} 
 	return result; 
 }, 

Or we remove the check and the pop completely.

I will create a PR when I know which solution is preferred.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
johnd0ecommented, Feb 16, 2021

I see (or it looks like) that you don’t really understand me and don’t think that it is a bug that it only hat 3 latlngs, but a rectangle is existing out of bounds, bounds are out of 4 latlngs. So why should it valid to remove the last latlng, when it is equal to the first one and then it has only 3 latlngs anymore.

Sure I understand you. You are making use of operating latlngs directly, and found some case that is not handled well in your way. While it’s very easy to solve your issue locally, I’m still not convinced that it will be useful for Leaflet in general. It’s obvious that Leaflet’s Rectangle is very limited and not even close to it’s math definition.

And we cannot just take SOME properties from that definition ignoring all others. And it’s even worse Leaflet shapes are screen only, not geodesic, so they keep their properties only in current projection.

Anyway, I am keeping this issue open, may be other maintainers have own opinions.

0reactions
Falke-Designcommented, Feb 15, 2021

Are you able to set non-horizontal Bounds?

No I’m not, because of this the method setLatLngs() is needed.

I can imagine that you need some rotated rectangles, but it is not purpose of L.Rectangle, by it’s definition.

I don’t think that it would be the right / correct way to say that rectangles can only horizontal Bounds. What would then the differences between Bounds and Rectangles (except the visualizing). No latlngs, only bounds can be set / get and is always horizontal. In my eyes then would it better to name it BoundsRectangle or something that make it clear that it is “connected” to bounds.

Math definition of a Rectangle:

  • has 4 sides / corners / angles
  • the sides are parallel
  • the length of opposide sides are equal
  • all angles are 90°

So there is no definition that it must be horizontal. Src 1 Src 2

I understand when we say that leaflet don’t support rotated rectangles but I think it should be possible that it can be set from outside for example with setLatLngs(). (By the way this is a nice method to create rotated rectangles with two opposite corners (like bounds) _getRotatedRectangle )

while rect.getBounds() returns same [[0,0],[0,0]])

Ok I don’t tried it with getBounds() … Then it is correct, but with getLatLngs() only 3 latlngs are returned.

I don’t know why do you need such zero-rectangle

This is a result of a editing function. When the user resizes the rectangle and drag the corners on top of each other, it can result in [[0,0],[0,0]]


I see (or it looks like) that you don’t really understand me and don’t think that it is a bug that it only hat 3 latlngs, but a rectangle is existing out of bounds, bounds are out of 4 latlngs. So why should it valid to remove the last latlng, when it is equal to the first one and then it has only 3 latlngs anymore. So I suggest to add this to L.Rectangle:

 _convertLatLngs: function (latlngs) { 
 	var result = Polyline.prototype._convertLatLngs.call(this, latlngs), 
 	    len = result.length; 
  
 	// remove last point if it equals first one 
 	if (len >= 4 && result[0] instanceof LatLng && result[0].equals(result[len - 1])) { 
 		result.pop(); 
 	} 
 	return result; 
 } 

No big change and it is never possible that leaflet destroys internally the rectangle latlngs. (It is something else if the user only pass 3 latlngs to setLatLng)

And in future maybe it has a better defenition but for now I think it is a bug …

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to convert a LatLng and a radius to a LatLngBounds in ...
The issue is that there is no way to create a LatLng and a radius into a LatLngBounds. The constructor for LatLngBounds only...
Read more >
L.Rectangle - WRLD3D
Returns true if the Polyline has no LatLngs. getCenter(), LatLng. Returns the center (centroid) of the polyline. getBounds(), LatLngBounds.
Read more >
Shapes | Maps JavaScript API - Google Developers
The Polygon in the example above consists of four sets of LatLng coordinates, but notice that the first and last sets define the...
Read more >
Documentation - a JavaScript library for interactive maps
Check this list if you are using a different version of Leaflet. ... when the map is initialized (when its center and zoom...
Read more >
v3.3.1 JavaScript Library: All | Mapbox.js
options, object, If provided, it is the same options as provided to L.Map with the ... selects the first result of a query...
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