Skip to content

Commit

Permalink
Sets area per default to square kilometers, closes area calculation bug
Browse files Browse the repository at this point in the history
  • Loading branch information
TimMcCauley authored and timmccauley committed Oct 15, 2018
1 parent cae00af commit b5716a8
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 86 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ target/
.python-version
nohup.out
build/
cgiar_cache/
4 changes: 3 additions & 1 deletion openrouteservice/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ TODO.txt
.project
#/WebContent/WEB-INF/app.config
*.iws
*.ipr
*.ipr
cgiar-cache
srtm_38_03.zip
54 changes: 26 additions & 28 deletions openrouteservice/src/main/java/heigit/ors/isochrones/Isochrone.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
/* This file is part of Openrouteservice.
*
* Openrouteservice is free software; you can redistribute it and/or modify it under the terms of the
* GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1
* Openrouteservice is free software; you can redistribute it and/or modify it under the terms of the
* GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License along with this library;
* if not, see <https://www.gnu.org/licenses/>.
* You should have received a copy of the GNU Lesser General Public License along with this library;
* if not, see <https://www.gnu.org/licenses/>.
*/
package heigit.ors.isochrones;

import java.util.ArrayList;
import java.util.List;

import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jts.geom.Geometry;

import heigit.ors.common.AttributeValue;
import heigit.ors.util.FormatUtility;
import heigit.ors.util.GeomUtility;
import heigit.ors.util.UnitsConverter;

import java.util.ArrayList;
import java.util.List;

public class Isochrone {
private Geometry geometry;
private double value;
Expand Down Expand Up @@ -57,32 +56,31 @@ public double getMaxRadius(String units)
case "mi":
return UnitsConverter.SqMetersToSqMiles(maxRadius);
case "km":
return UnitsConverter.SqMetersToSqKilometers(maxRadius);
return UnitsConverter.SqMetersToSqKilometers(maxRadius);
}
}

return maxRadius;
}

public double getArea(String units) throws Exception
{
double area = getArea(true);
public double getArea(String units) throws Exception {
double area = getArea(true);
if (units != null) {
switch (units) {
case "m":
return area;
case "mi":
return UnitsConverter.SqMetersToSqMiles(area);
case "km":
return UnitsConverter.SqMetersToSqKilometers(area);
}
}

if (units != null)
{
switch(units)
{
case "m":
return area;
case "mi":
return UnitsConverter.SqMetersToSqMiles(area);
case "km":
return UnitsConverter.SqMetersToSqKilometers(area);
}
}

return area;
}
// return default square km
return UnitsConverter.SqMetersToSqKilometers(area);

}

public double getArea(Boolean inMeters) throws Exception {
if (area == 0.0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,8 @@
*/
package heigit.ors.services.isochrones.requestprocessors.json;

import java.io.InputStream;
import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONArray;
import org.json.JSONObject;

import com.graphhopper.util.Helper;
import com.vividsolutions.jts.geom.Coordinate;

import heigit.ors.common.StatusCode;
import heigit.ors.common.TravelRangeType;
import heigit.ors.common.TravellerInfo;
Expand All @@ -37,6 +28,12 @@
import heigit.ors.services.isochrones.IsochronesServiceSettings;
import heigit.ors.util.CoordTools;
import heigit.ors.util.StreamUtility;
import org.json.JSONArray;
import org.json.JSONObject;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.Arrays;

public class JsonIsochroneRequestParser {

Expand Down Expand Up @@ -304,21 +301,16 @@ public static IsochroneRequest parseFromRequestParams(HttpServletRequest request
}
}

value = request.getParameter("units");
if (!Helper.isEmpty(value))
{
if (travellerInfo.getRangeType() == TravelRangeType.Distance)
{
if (!("m".equals(value) || "km".equals(value) || "mi".equals(value)))
throw new UnknownParameterValueException(IsochronesErrorCodes.INVALID_PARAMETER_VALUE, "units", value);
}
else
{
throw new ParameterValueException(IsochronesErrorCodes.INVALID_PARAMETER_VALUE, "units");
}
value = request.getParameter("units");
if (!Helper.isEmpty(value)) {

req.setUnits(value.toLowerCase());
}

if (!("m".equals(value) || "km".equals(value) || "mi".equals(value)))
throw new UnknownParameterValueException(IsochronesErrorCodes.INVALID_PARAMETER_VALUE, "units", value);


req.setUnits(value.toLowerCase());
}

boolean inverseXY = false;
value = request.getParameter("locations");
Expand Down
63 changes: 29 additions & 34 deletions openrouteservice/src/main/java/heigit/ors/util/GeomUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,42 +176,37 @@ public static double degreesToMetres(double degrees) {
return degrees * 111139;
}

public static double getArea(Geometry geom, Boolean inMeters) throws Exception
{
if (inMeters) {
if (geom instanceof Polygon)
{
Polygon poly = (Polygon) geom;
double area = Math.abs(getSignedArea(poly.getExteriorRing().getCoordinateSequence()));

for (int i = 0; i < poly.getNumInteriorRing(); i++) {
LineString hole = poly.getInteriorRingN(i);
area -= Math.abs(getSignedArea(hole.getCoordinateSequence()));
}

return area;
}
else if (geom instanceof LineString)
{
LineString ring = (LineString)geom;
return getSignedArea(ring.getCoordinateSequence());
}
else
{
if (TRANSFORM_WGS84_SPHERICALMERCATOR == null) {
String wkt = "PROJCS[\"WGS 84 / Pseudo-Mercator\",GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]],PROJECTION[\"Mercator_1SP\"],PARAMETER[\"central_meridian\",0],PARAMETER[\"scale_factor\",1],PARAMETER[\"false_easting\",0],PARAMETER[\"false_northing\",0],UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],AXIS[\"X\",EAST],AXIS[\"Y\",NORTH],AUTHORITY[\"EPSG\",\"3857\"]]";
CoordinateReferenceSystem crs = CRS.parseWKT(wkt);// CRS.decode("EPSG:3857");
TRANSFORM_WGS84_SPHERICALMERCATOR = CRS.findMathTransform(DefaultGeographicCRS.WGS84, crs, true);
}
public static double getArea(Geometry geom, Boolean inMeters) throws Exception {
if (inMeters) {
if (geom instanceof Polygon) {
Polygon poly = (Polygon) geom;

double area = Math.abs(getSignedArea(poly.getExteriorRing().getCoordinateSequence()));

Geometry transformedGeometry = JTS.transform(geom, TRANSFORM_WGS84_SPHERICALMERCATOR);
return transformedGeometry.getArea();
}
} else {
return geom.getArea();
}
}
for (int i = 0; i < poly.getNumInteriorRing(); i++) {
LineString hole = poly.getInteriorRingN(i);
area -= Math.abs(getSignedArea(hole.getCoordinateSequence()));
}

return area;
} else if (geom instanceof LineString) {
LineString ring = (LineString) geom;
return getSignedArea(ring.getCoordinateSequence());
} else {
if (TRANSFORM_WGS84_SPHERICALMERCATOR == null) {
String wkt = "PROJCS[\"WGS 84 / Pseudo-Mercator\",GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]],PROJECTION[\"Mercator_1SP\"],PARAMETER[\"central_meridian\",0],PARAMETER[\"scale_factor\",1],PARAMETER[\"false_easting\",0],PARAMETER[\"false_northing\",0],UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],AXIS[\"X\",EAST],AXIS[\"Y\",NORTH],AUTHORITY[\"EPSG\",\"3857\"]]";
CoordinateReferenceSystem crs = CRS.parseWKT(wkt);// CRS.decode("EPSG:3857");
TRANSFORM_WGS84_SPHERICALMERCATOR = CRS.findMathTransform(DefaultGeographicCRS.WGS84, crs, true);
}


Geometry transformedGeometry = JTS.transform(geom, TRANSFORM_WGS84_SPHERICALMERCATOR);
return transformedGeometry.getArea();
}
} else {
return geom.getArea();
}
}

/**
* Determine the 2D bearing between two points. Note that this does not take into account the spheroid shape of
Expand Down

0 comments on commit b5716a8

Please sign in to comment.