Search in sources :

Example 1 with GeoJsonObject

use of org.geojson.GeoJsonObject in project OpenTripPlanner by opentripplanner.

the class BanoGeocoder method geocode.

/**
 */
@Override
public GeocoderResults geocode(String address, Envelope bbox) {
    try {
        URL banoUrl = getBanoGeocoderUrl(address, bbox);
        URLConnection conn = banoUrl.openConnection();
        InputStream in = conn.getInputStream();
        FeatureCollection featureCollection = mapper.readValue(in, FeatureCollection.class);
        in.close();
        List<GeocoderResult> geocoderResults = new ArrayList<GeocoderResult>();
        for (Feature feature : featureCollection.getFeatures()) {
            GeoJsonObject geom = feature.getGeometry();
            if (geom instanceof Point) {
                Point p = (Point) geom;
                GeocoderResult res = new GeocoderResult();
                res.setLat(p.getCoordinates().getLatitude());
                res.setLng(p.getCoordinates().getLongitude());
                res.setDescription(feature.getProperties().get("label").toString());
                /*
                     * Note: We also have here as properties a break-down of other details, such as
                     * the house number, street, city, postcode... Can be useful if needed.
                     */
                geocoderResults.add(res);
            } else {
            // Should not happen according to the API
            }
        }
        return new GeocoderResults(geocoderResults);
    } catch (IOException e) {
        LOG.error("Error processing BANO geocoder results", e);
        return new GeocoderResults(e.getLocalizedMessage());
    }
}
Also used : GeocoderResults(org.opentripplanner.geocoder.GeocoderResults) FeatureCollection(org.geojson.FeatureCollection) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Point(org.geojson.Point) IOException(java.io.IOException) GeoJsonObject(org.geojson.GeoJsonObject) DeserializationFeature(com.fasterxml.jackson.databind.DeserializationFeature) Feature(org.geojson.Feature) URL(java.net.URL) URLConnection(java.net.URLConnection) GeocoderResult(org.opentripplanner.geocoder.GeocoderResult)

Example 2 with GeoJsonObject

use of org.geojson.GeoJsonObject in project dhis2-core by dhis2.

the class AttributeValueServiceTest method testGeoJSONAttributeValue.

@Test
void testGeoJSONAttributeValue() throws JsonProcessingException {
    Attribute attribute = new Attribute();
    attribute.setValueType(ValueType.GEOJSON);
    attribute.setName("attributeGeoJson");
    attributeService.addAttribute(attribute);
    String geoJson = "{\"type\": \"Feature\", \"geometry\": { \"type\": \"Point\"," + "\"coordinates\": [125.6, 10.1] }, \"properties\": { \"name\": \"Dinagat Islands\" } }";
    AttributeValue avA = new AttributeValue(geoJson, attribute);
    dataElementA.getAttributeValues().add(avA);
    dataElementStore.save(dataElementA);
    List<DataElement> dataElements = dataElementStore.getByAttribute(attribute);
    assertEquals(1, dataElements.size());
    assertEquals(dataElementA.getUid(), dataElements.get(0).getUid());
    dataElements = dataElementStore.getByAttributeAndValue(attribute, geoJson);
    assertEquals(1, dataElements.size());
    assertEquals(dataElementA.getUid(), dataElements.get(0).getUid());
    DataElement dataElement = dataElements.get(0);
    AttributeValue av = dataElement.getAttributeValues().iterator().next();
    GeoJsonObject geoJsonObject = new ObjectMapper().readValue(av.getValue(), GeoJsonObject.class);
    assertTrue(geoJsonObject instanceof Feature);
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) GeoJsonObject(org.geojson.GeoJsonObject) Feature(org.geojson.Feature) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 3 with GeoJsonObject

use of org.geojson.GeoJsonObject in project dhis2-core by dhis2.

the class GeoFeatureService method getCoordinates.

/**
 * Get the {@link GeoFeature} coordinate from {@link DimensionalItemObject}
 * <p>
 * The coordinate value is retrieved from {@link DimensionalItemObject}'s
 * geoJsonAttribute value.
 *
 * @param feature the {@link GeoFeature}
 * @param unit the {@link DimensionalItemObject} contains the coordinate
 *        values.
 * @param geoJsonAttribute The {@link Attribute} which has
 *        {@link ValueType#GEOJSON} and is assigned to
 *        {@link OrganisationUnit}.
 * @return the given {@link GeoFeature} with updated coordinate value and
 *         coordinate type.
 */
private void getCoordinates(GeoFeature feature, DimensionalItemObject unit, Attribute geoJsonAttribute) {
    if (geoJsonAttribute == null) {
        getCoordinates(feature, unit);
        return;
    }
    if (!unit.getClass().isAssignableFrom(OrganisationUnit.class)) {
        return;
    }
    OrganisationUnit organisationUnit = (OrganisationUnit) unit;
    Optional<AttributeValue> geoJsonAttributeValue = organisationUnit.getAttributeValues().stream().filter(attributeValue -> attributeValue.getAttribute().getUid().equals(geoJsonAttribute.getUid())).findFirst();
    if (!geoJsonAttributeValue.isPresent() || StringUtils.isBlank(geoJsonAttributeValue.get().getValue())) {
        getCoordinates(feature, unit);
        return;
    }
    try {
        GeoJsonObject geoJsonObject = new ObjectMapper().readValue(geoJsonAttributeValue.get().getValue(), GeoJsonObject.class);
        GeoFeature geoJsonFeature = geoJsonObject.accept(new GeoFeatureVisitor());
        if (geoJsonFeature == null) {
            return;
        }
        feature.setTy(geoJsonFeature.getTy());
        feature.setCo(geoJsonFeature.getCo());
    } catch (JsonProcessingException e) {
        log.error(String.format("Couldn't read GeoJson value from organisationUnit %s: ", organisationUnit), e);
        getCoordinates(feature, unit);
    }
}
Also used : Date(java.util.Date) ValueType(org.hisp.dhis.common.ValueType) ValidationUtils(org.hisp.dhis.system.util.ValidationUtils) GeoFeature(org.hisp.dhis.webapi.webdomain.GeoFeature) StringUtils(org.apache.commons.lang3.StringUtils) MultiPolygon(org.geojson.MultiPolygon) MultiLineString(org.geojson.MultiLineString) FeatureCollection(org.geojson.FeatureCollection) CoordinateObject(org.hisp.dhis.common.coordinate.CoordinateObject) Map(java.util.Map) OrganisationUnitGroupService(org.hisp.dhis.organisationunit.OrganisationUnitGroupService) ImmutableMap(com.google.common.collect.ImmutableMap) GeoJsonObject(org.geojson.GeoJsonObject) GeoJsonObjectVisitor(org.geojson.GeoJsonObjectVisitor) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) OrganisationUnitGroup(org.hisp.dhis.organisationunit.OrganisationUnitGroup) Set(java.util.Set) Collectors(java.util.stream.Collectors) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Builder(lombok.Builder) AttributeService(org.hisp.dhis.attribute.AttributeService) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) DimensionalObject(org.hisp.dhis.common.DimensionalObject) Optional(java.util.Optional) DhisApiVersion(org.hisp.dhis.common.DhisApiVersion) ORGUNIT_DIM_ID(org.hisp.dhis.common.DimensionalObject.ORGUNIT_DIM_ID) ORGUNIT_GROUP_DIM_ID(org.hisp.dhis.common.DimensionalObject.ORGUNIT_GROUP_DIM_ID) DataQueryService(org.hisp.dhis.analytics.DataQueryService) AttributeValue(org.hisp.dhis.attribute.AttributeValue) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) Getter(lombok.Getter) Feature(org.geojson.Feature) Attribute(org.hisp.dhis.attribute.Attribute) ArrayList(java.util.ArrayList) GeometryCollection(org.geojson.GeometryCollection) HashSet(java.util.HashSet) HttpServletRequest(javax.servlet.http.HttpServletRequest) Lists(com.google.common.collect.Lists) LineString(org.geojson.LineString) Service(org.springframework.stereotype.Service) DisplayProperty(org.hisp.dhis.common.DisplayProperty) DataQueryRequest(org.hisp.dhis.common.DataQueryRequest) Polygon(org.geojson.Polygon) DimensionalObjectUtils(org.hisp.dhis.common.DimensionalObjectUtils) ContextUtils(org.hisp.dhis.webapi.utils.ContextUtils) HttpServletResponse(javax.servlet.http.HttpServletResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) AggregationType(org.hisp.dhis.analytics.AggregationType) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) OrganisationUnitGroupSet(org.hisp.dhis.organisationunit.OrganisationUnitGroupSet) MultiPoint(org.geojson.MultiPoint) DebugUtils(org.hisp.dhis.commons.util.DebugUtils) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ObjectUtils(org.hisp.dhis.util.ObjectUtils) FeatureType(org.hisp.dhis.organisationunit.FeatureType) CurrentUserService(org.hisp.dhis.user.CurrentUserService) Point(org.geojson.Point) Comparator(java.util.Comparator) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) AttributeValue(org.hisp.dhis.attribute.AttributeValue) GeoJsonObject(org.geojson.GeoJsonObject) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) GeoFeature(org.hisp.dhis.webapi.webdomain.GeoFeature)

Example 4 with GeoJsonObject

use of org.geojson.GeoJsonObject in project dhis2-core by dhis2.

the class MetadataImportExportControllerTest method testPostValidGeoJsonAttribute.

@Test
void testPostValidGeoJsonAttribute() throws IOException {
    POST("/metadata", "{\"organisationUnits\": [ {\"id\":\"rXnqqH2Pu6N\",\"name\": \"My Unit 2\",\"shortName\": \"OU2\",\"openingDate\": \"2020-01-01\"," + "\"attributeValues\": [{\"value\":  \"{\\\"type\\\": \\\"Polygon\\\"," + "\\\"coordinates\\\":  [[[100,0],[101,0],[101,1],[100,1],[100,0]]] }\"," + "\"attribute\": {\"id\": \"RRH9IFiZZYN\"}}]}]," + "\"attributes\":[{\"id\":\"RRH9IFiZZYN\",\"valueType\":\"GEOJSON\",\"organisationUnitAttribute\":true,\"name\":\"testgeojson\"}]}").content(HttpStatus.OK);
    JsonIdentifiableObject organisationUnit = GET("/organisationUnits/{id}", "rXnqqH2Pu6N").content().asObject(JsonIdentifiableObject.class);
    assertEquals(1, organisationUnit.getAttributeValues().size());
    JsonAttributeValue attributeValue = organisationUnit.getAttributeValues().get(0);
    GeoJsonObject geoJSON = new ObjectMapper().readValue(attributeValue.getValue(), GeoJsonObject.class);
    assertTrue(geoJSON instanceof Polygon);
    Polygon polygon = (Polygon) geoJSON;
    assertEquals(100, polygon.getCoordinates().get(0).get(0).getLongitude());
}
Also used : JsonIdentifiableObject(org.hisp.dhis.webapi.json.domain.JsonIdentifiableObject) JsonAttributeValue(org.hisp.dhis.webapi.json.domain.JsonAttributeValue) GeoJsonObject(org.geojson.GeoJsonObject) Polygon(org.geojson.Polygon) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DhisControllerConvenienceTest(org.hisp.dhis.webapi.DhisControllerConvenienceTest) Test(org.junit.jupiter.api.Test)

Example 5 with GeoJsonObject

use of org.geojson.GeoJsonObject in project geojson-jackson by opendatalab-de.

the class PointTest method itShouldDeserializeAPointWithAltitude.

@Test
public void itShouldDeserializeAPointWithAltitude() throws Exception {
    GeoJsonObject value = mapper.readValue("{\"type\":\"Point\",\"coordinates\":[100.0,5.0,123]}", GeoJsonObject.class);
    Point point = (Point) value;
    assertLngLatAlt(100, 5, 123, point.getCoordinates());
}
Also used : Point(org.geojson.Point) GeoJsonObject(org.geojson.GeoJsonObject) Test(org.junit.Test)

Aggregations

GeoJsonObject (org.geojson.GeoJsonObject)8 Point (org.geojson.Point)5 Test (org.junit.Test)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Feature (org.geojson.Feature)3 ArrayList (java.util.ArrayList)2 FeatureCollection (org.geojson.FeatureCollection)2 Polygon (org.geojson.Polygon)2 Test (org.junit.jupiter.api.Test)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 DeserializationFeature (com.fasterxml.jackson.databind.DeserializationFeature)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Lists (com.google.common.collect.Lists)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 Comparator (java.util.Comparator)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1