Search in sources :

Example 1 with JsonFeatureCollection

use of com.graphhopper.util.JsonFeatureCollection in project graphhopper by graphhopper.

the class JsonFeatureCollectionTest method testDeserialization.

@Test
public void testDeserialization() throws IOException {
    JsonFeatureCollection data = objectMapper.readValue(getClass().getClassLoader().getResourceAsStream("fixtures/geojson1.json"), JsonFeatureCollection.class);
    assertEquals(3, data.getFeatures().size());
    JsonFeature f1 = data.getFeatures().get(0);
    assertEquals("1", f1.getId());
    assertEquals("value0", f1.getProperty("prop0"));
    assertEquals(0.5, f1.getGeometry().getCoordinate().y, .1);
    assertEquals(102.0, f1.getGeometry().getCoordinate().x, .1);
    JsonFeature f2 = data.getFeatures().get(1);
    // read as string despite the 2 (not a string) in json
    assertEquals("2", f2.getId());
    assertEquals(4, f2.getGeometry().getNumPoints());
    assertEquals(0.0, PointList.fromLineString((LineString) f2.getGeometry()).getLat(0), .1);
    assertEquals(102.0, PointList.fromLineString((LineString) f2.getGeometry()).getLon(0), .1);
    assertEquals(1.0, PointList.fromLineString((LineString) f2.getGeometry()).getLat(1), .1);
    assertEquals(103.0, PointList.fromLineString((LineString) f2.getGeometry()).getLon(1), .1);
    JsonFeature f3 = data.getFeatures().get(2);
    assertEquals(0.0, f3.getBBox().getMinY(), 0.0);
    assertEquals(102.0, f3.getBBox().getMinX(), 0.0);
    assertEquals(1.0, f3.getBBox().getMaxY(), 0.0);
    assertEquals(103.0, f3.getBBox().getMaxX(), 0.0);
    assertEquals("a", ((Map) f3.getProperty("prop1")).get("test"));
}
Also used : JsonFeatureCollection(com.graphhopper.util.JsonFeatureCollection) JsonFeature(com.graphhopper.util.JsonFeature) Test(org.junit.jupiter.api.Test)

Example 2 with JsonFeatureCollection

use of com.graphhopper.util.JsonFeatureCollection in project graphhopper by graphhopper.

the class JsonFeatureCollectionTest method testSerialization.

@Test
public void testSerialization() throws IOException {
    GeometryFactory geometryFactory = new GeometryFactory();
    JsonFeatureCollection jsonFeatureCollection = new JsonFeatureCollection();
    {
        JsonFeature jsonFeature = new JsonFeature();
        jsonFeature.setId("1");
        HashMap<String, Object> properties = new HashMap<>();
        properties.put("prop0", "value0");
        jsonFeature.setProperties(properties);
        jsonFeature.setGeometry(geometryFactory.createPoint(new Coordinate(102.0, 0.5)));
        jsonFeatureCollection.getFeatures().add(jsonFeature);
    }
    {
        JsonFeature jsonFeature = new JsonFeature();
        jsonFeature.setId("2");
        Map<String, Object> properties = new LinkedHashMap<>();
        properties.put("prop0", "value1");
        properties.put("prop1", 2);
        jsonFeature.setProperties(properties);
        jsonFeature.setGeometry(geometryFactory.createLineString(new Coordinate[] { new Coordinate(102.0, 0.0), new Coordinate(103.0, 1.0), new Coordinate(104.0, 0.0), new Coordinate(105.0, 1.0) }));
        jsonFeatureCollection.getFeatures().add(jsonFeature);
    }
    {
        JsonFeature jsonFeature = new JsonFeature();
        jsonFeature.setId("3");
        Map<String, Object> properties = new LinkedHashMap<>();
        properties.put("prop0", "value0");
        Map<String, String> prop1 = new LinkedHashMap<>();
        prop1.put("test", "a");
        properties.put("prop1", prop1);
        jsonFeature.setProperties(properties);
        jsonFeature.setBBox(new Envelope(102.0, 103.0, 0.0, 1));
        jsonFeatureCollection.getFeatures().add(jsonFeature);
    }
    String expected = objectMapper.writeValueAsString(objectMapper.readValue(getClass().getClassLoader().getResourceAsStream("fixtures/geojson1.json"), JsonFeatureCollection.class));
    assertEquals(objectMapper.writeValueAsString(jsonFeatureCollection), expected);
}
Also used : GeometryFactory(org.locationtech.jts.geom.GeometryFactory) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Coordinate(org.locationtech.jts.geom.Coordinate) JsonFeatureCollection(com.graphhopper.util.JsonFeatureCollection) LineString(org.locationtech.jts.geom.LineString) Envelope(org.locationtech.jts.geom.Envelope) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) JsonFeature(com.graphhopper.util.JsonFeature) Test(org.junit.jupiter.api.Test)

Example 3 with JsonFeatureCollection

use of com.graphhopper.util.JsonFeatureCollection in project graphhopper by graphhopper.

the class IsochroneResourceTest method requestTenBucketsIssue2094.

@Test
public void requestTenBucketsIssue2094() {
    Response response = clientTarget(app, "/isochrone?profile=fast_car&point=42.510008,1.530018&time_limit=400&type=geojson&buckets=10").request().buildGet().invoke();
    JsonFeatureCollection collection = response.readEntity(JsonFeatureCollection.class);
    Polygon lastPolygon = (Polygon) collection.getFeatures().get(collection.getFeatures().size() - 1).getGeometry();
    assertTrue(lastPolygon.contains(geometryFactory.createPoint(new Coordinate(1.580229, 42.533161))));
    assertFalse(lastPolygon.contains(geometryFactory.createPoint(new Coordinate(1.584606, 42.535121))));
    Polygon beforeLastPolygon = (Polygon) collection.getFeatures().get(collection.getFeatures().size() - 2).getGeometry();
    assertTrue(beforeLastPolygon.contains(geometryFactory.createPoint(new Coordinate(1.564136, 42.524938))));
    assertFalse(beforeLastPolygon.contains(geometryFactory.createPoint(new Coordinate(1.571474, 42.529176))));
}
Also used : Response(javax.ws.rs.core.Response) Coordinate(org.locationtech.jts.geom.Coordinate) JsonFeatureCollection(com.graphhopper.util.JsonFeatureCollection) Polygon(org.locationtech.jts.geom.Polygon) Test(org.junit.jupiter.api.Test)

Example 4 with JsonFeatureCollection

use of com.graphhopper.util.JsonFeatureCollection in project graphhopper by graphhopper.

the class IsochroneResourceTest method requestByWeightLimit.

@Test
public void requestByWeightLimit() {
    WebTarget commonTarget = clientTarget(app, "/isochrone").queryParam("profile", "short_car").queryParam("point", "42.531073,1.573792").queryParam("type", "geojson");
    long limit = 3000;
    Response distanceLimitRsp = commonTarget.queryParam("distance_limit", limit).request().buildGet().invoke();
    JsonFeatureCollection distanceLimitFeatureCollection = distanceLimitRsp.readEntity(JsonFeatureCollection.class);
    Geometry distanceLimitPolygon = distanceLimitFeatureCollection.getFeatures().get(0).getGeometry();
    Response weightLimitRsp = commonTarget.queryParam("weight_limit", limit).request().buildGet().invoke();
    JsonFeatureCollection weightLimitFeatureCollection = weightLimitRsp.readEntity(JsonFeatureCollection.class);
    Geometry weightLimitPolygon = weightLimitFeatureCollection.getFeatures().get(0).getGeometry();
    assertEquals(distanceLimitPolygon.getNumPoints(), weightLimitPolygon.getNumPoints());
    assertTrue(weightLimitPolygon.equalsTopo(distanceLimitPolygon));
}
Also used : Response(javax.ws.rs.core.Response) Geometry(org.locationtech.jts.geom.Geometry) JsonFeatureCollection(com.graphhopper.util.JsonFeatureCollection) WebTarget(javax.ws.rs.client.WebTarget) Test(org.junit.jupiter.api.Test)

Example 5 with JsonFeatureCollection

use of com.graphhopper.util.JsonFeatureCollection in project graphhopper by graphhopper.

the class IsochroneResourceTest method requestByDistanceLimit.

@Test
public void requestByDistanceLimit() {
    Response rsp = clientTarget(app, "/isochrone").queryParam("profile", "fast_car").queryParam("point", "42.531073,1.573792").queryParam("distance_limit", 3_000).queryParam("buckets", 2).queryParam("type", "geojson").request().buildGet().invoke();
    JsonFeatureCollection featureCollection = rsp.readEntity(JsonFeatureCollection.class);
    assertEquals(2, featureCollection.getFeatures().size());
    Geometry polygon0 = featureCollection.getFeatures().get(0).getGeometry();
    Geometry polygon1 = featureCollection.getFeatures().get(1).getGeometry();
    assertTrue(polygon0.contains(geometryFactory.createPoint(new Coordinate(1.57937, 42.531706))));
    assertFalse(polygon0.contains(geometryFactory.createPoint(new Coordinate(1.587224, 42.5386))));
    assertTrue(polygon1.contains(geometryFactory.createPoint(new Coordinate(1.591644, 42.543216))));
    assertFalse(polygon1.contains(geometryFactory.createPoint(new Coordinate(1.589756, 42.558012))));
}
Also used : Response(javax.ws.rs.core.Response) Geometry(org.locationtech.jts.geom.Geometry) Coordinate(org.locationtech.jts.geom.Coordinate) JsonFeatureCollection(com.graphhopper.util.JsonFeatureCollection) Test(org.junit.jupiter.api.Test)

Aggregations

JsonFeatureCollection (com.graphhopper.util.JsonFeatureCollection)12 Test (org.junit.jupiter.api.Test)10 Response (javax.ws.rs.core.Response)8 Coordinate (org.locationtech.jts.geom.Coordinate)8 Geometry (org.locationtech.jts.geom.Geometry)7 JsonFeature (com.graphhopper.util.JsonFeature)2 JtsModule (com.bedatadriven.jackson.datatype.jts.JtsModule)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 WebTarget (javax.ws.rs.client.WebTarget)1 Envelope (org.locationtech.jts.geom.Envelope)1 GeometryFactory (org.locationtech.jts.geom.GeometryFactory)1 LineString (org.locationtech.jts.geom.LineString)1