Skip to content

Commit

Permalink
#16 do not initialize southWest and northEast with 0;0 in LatLngBounds
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabor committed Jul 2, 2020
1 parent fe50fd8 commit 848d531
Showing 1 changed file with 174 additions and 159 deletions.
333 changes: 174 additions & 159 deletions src/main/java/com/vaadin/addon/leaflet4vaadin/types/LatLngBounds.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,163 +31,178 @@
* @version 1.0
*/
public class LatLngBounds implements BasicType {
private static final long serialVersionUID = -7421430446913242834L;

@JsonProperty("_southWest")
private LatLng southWest = new LatLng();
@JsonProperty("_northEast")
private LatLng northEast = new LatLng();

public LatLngBounds() {
}

public LatLngBounds(LatLng... latlngs) {
this(Arrays.asList(latlngs));
}

public LatLngBounds(List<LatLng> latlngs) {
this(latlngs.get(0));
latlngs.forEach(p -> extend(p));
}

public LatLngBounds(LatLng latlng) {
southWest.setLat(latlng.getLat());
southWest.setLng(latlng.getLng());
northEast.setLat(latlng.getLat());
northEast.setLng(latlng.getLng());
}

/**
* @param southWest the southWest to set
*/
public void setSouthWest(LatLng southWest) {
this.southWest = southWest;
}

/**
* @param northEast the northEast to set
*/
public void setNorthEast(LatLng northEast) {
this.northEast = northEast;
}

/**
* Returns the south-west point of the bounds.
*
* @return the south-west point of the bounds.
*/
public LatLng getSouthWest() {
return this.southWest;
}

/**
* Returns the south-east point of the bounds.
*
* @return the south-east point of the bounds
*/
@JsonIgnore
public LatLng getSouthEast() {
return new LatLng(this.getSouth(), this.getEast());
}

/**
* Returns the north-east point of the bounds.
*
* @return the north-east point of the bounds.
*/
public LatLng getNorthEast() {
return this.northEast;
}

/**
* Returns the north-west point of the bounds.
*
* @return Returns the north-west point of the bounds
*/
@JsonIgnore
public LatLng getNorthWest() {
return new LatLng(this.getNorth(), this.getWest());
}

/**
* Returns the west longitude of the bounds
*
* @return the west longitude of the bounds
*/
@JsonIgnore
public double getWest() {
return this.southWest.getLng();
}

/**
* Returns the south latitude of the bounds
*
* @return the south latitude of the bounds
*/
@JsonIgnore
public double getSouth() {
return this.southWest.getLat();
}

/**
* Returns the east longitude of the bounds
*
* @return the east longitude of the bounds
*/
@JsonIgnore
public double getEast() {
return this.northEast.getLng();
}

/**
* Returns the north latitude of the bounds
*
* @return the north latitude of the bounds
*/
@JsonIgnore
public double getNorth() {
return this.northEast.getLat();
}

/**
* Returns the center point of the bounds.
*
* @return the center point of the bounds.
*/
@JsonIgnore
public LatLng getCenter() {
double lat = (this.southWest.getLat() + this.northEast.getLat()) / 2;
double lon = (this.southWest.getLng() + this.northEast.getLng()) / 2;
return new LatLng(lat, lon);
}

public void extend(LatLng... latlngs) {
extend(Arrays.asList(latlngs));
}

public void extend(List<LatLng> latlngs) {
for (LatLng latlng : latlngs) {
northEast.setLat(Math.max(northEast.getLat(), latlng.getLat()));
northEast.setLng(Math.max(northEast.getLng(), latlng.getLng()));
southWest.setLat(Math.min(southWest.getLat(), latlng.getLat()));
southWest.setLng(Math.min(southWest.getLng(), latlng.getLng()));
}
}

@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}

/**
* Extend the bounds to contain the given bounds
*
* @param bounds the bounds
*/
public void extend(LatLngBounds bounds) {
northEast.setLat(Math.max(northEast.getLat(), bounds.getSouthWest().getLat()));
northEast.setLng(Math.max(northEast.getLng(), bounds.getSouthWest().getLng()));
southWest.setLat(Math.min(southWest.getLat(), bounds.getNorthEast().getLat()));
southWest.setLng(Math.min(southWest.getLng(), bounds.getNorthEast().getLng()));
}
private static final long serialVersionUID = -7421430446913242834L;

@JsonProperty("_southWest")
private LatLng southWest;
@JsonProperty("_northEast")
private LatLng northEast;

public LatLngBounds(LatLng... latlngs) {
this(Arrays.asList(latlngs));
}

public LatLngBounds(List<LatLng> latlngs) {
extend(latlngs);
}

public LatLngBounds(LatLng latlng) {
southWest = new LatLng(latlng.getLat(), latlng.getLng());
northEast = new LatLng(latlng.getLat(), latlng.getLng());
}

/**
* @param southWest
* the southWest to set
*/
public void setSouthWest(LatLng southWest) {
this.southWest = southWest;
}

/**
* @param northEast
* the northEast to set
*/
public void setNorthEast(LatLng northEast) {
this.northEast = northEast;
}

/**
* Returns the south-west point of the bounds.
*
* @return the south-west point of the bounds.
*/
public LatLng getSouthWest() {
return this.southWest;
}

/**
* Returns the south-east point of the bounds.
*
* @return the south-east point of the bounds
*/
@JsonIgnore
public LatLng getSouthEast() {
return new LatLng(this.getSouth(), this.getEast());
}

/**
* Returns the north-east point of the bounds.
*
* @return the north-east point of the bounds.
*/
public LatLng getNorthEast() {
return this.northEast;
}

/**
* Returns the north-west point of the bounds.
*
* @return Returns the north-west point of the bounds
*/
@JsonIgnore
public LatLng getNorthWest() {
return new LatLng(this.getNorth(), this.getWest());
}

/**
* Returns the west longitude of the bounds
*
* @return the west longitude of the bounds
*/
@JsonIgnore
public double getWest() {
return this.southWest.getLng();
}

/**
* Returns the south latitude of the bounds
*
* @return the south latitude of the bounds
*/
@JsonIgnore
public double getSouth() {
return this.southWest.getLat();
}

/**
* Returns the east longitude of the bounds
*
* @return the east longitude of the bounds
*/
@JsonIgnore
public double getEast() {
return this.northEast.getLng();
}

/**
* Returns the north latitude of the bounds
*
* @return the north latitude of the bounds
*/
@JsonIgnore
public double getNorth() {
return this.northEast.getLat();
}

/**
* Returns the center point of the bounds.
*
* @return the center point of the bounds.
*/
@JsonIgnore
public LatLng getCenter() {
double lat = (this.southWest.getLat() + this.northEast.getLat()) / 2;
double lon = (this.southWest.getLng() + this.northEast.getLng()) / 2;
return new LatLng(lat, lon);
}

public void extend(LatLng... latlngs) {
extend(Arrays.asList(latlngs));
}

public void extend(List<LatLng> latlngs) {
if (latlngs != null && !latlngs.isEmpty()) {
LatLng latLng = latlngs.get(0);
if (northEast == null) {
northEast = new LatLng(latLng.getLat(), latLng.getLng());
}
if (southWest == null) {
southWest = new LatLng(latLng.getLat(), latLng.getLng());
}
for (LatLng latlng : latlngs) {
northEast.setLat(Math.max(northEast.getLat(), latlng.getLat()));
northEast.setLng(Math.max(northEast.getLng(), latlng.getLng()));
southWest.setLat(Math.min(southWest.getLat(), latlng.getLat()));
southWest.setLng(Math.min(southWest.getLng(), latlng.getLng()));
}
}
}

@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}

/**
* Extend the bounds to contain the given bounds
*
* @param bounds
* the bounds
*/
public void extend(LatLngBounds bounds) {
if (bounds != null) {
if (northEast == null) {
northEast = new LatLng(bounds.getNorth(), bounds.getEast());
}
if (southWest == null) {
southWest = new LatLng(bounds.getSouth(), bounds.getWest());
}
northEast.setLat(Math.max(northEast.getLat(), bounds.getSouthWest().getLat()));
northEast.setLng(Math.max(northEast.getLng(), bounds.getSouthWest().getLng()));
southWest.setLat(Math.min(southWest.getLat(), bounds.getNorthEast().getLat()));
southWest.setLng(Math.min(southWest.getLng(), bounds.getNorthEast().getLng()));
}
}

}

0 comments on commit 848d531

Please sign in to comment.