Search in sources :

Example 6 with Feature

use of org.geojson.Feature 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)

Aggregations

Feature (org.geojson.Feature)6 FeatureCollection (org.geojson.FeatureCollection)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 ArrayList (java.util.ArrayList)3 GeoJsonObject (org.geojson.GeoJsonObject)3 IOException (java.io.IOException)2 Map (java.util.Map)2 MultiPoint (org.geojson.MultiPoint)2 Point (org.geojson.Point)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 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 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1