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());
}
}
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);
}
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);
}
}
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());
}
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());
}
Aggregations