@SuppressWarnings("unchecked") public static PointFeature fromJsonNode(JsonNode feature) throws EmptyPolygonException, UnsupportedGeometryException { Feature geoJsonFeature; try { geoJsonFeature = deserializer.readValue(feature.traverse(), Feature.class); } catch (IOException e) { throw new UnsupportedGeometryException(e.getMessage()); } PointFeature ret = new PointFeature(geoJsonFeature.getId()); ret.setGeom(GeometryUtils.convertGeoJsonToJtsGeometry(geoJsonFeature.getGeometry())); Object structured = geoJsonFeature.getProperty("structured"); if (structured == null || !(structured instanceof Map)) return null; // The code below assume the structured map to have integers only ret.setAttributes((Map<String, Integer>)(structured)); return ret; }
GeoJsonObject geom = feature.getGeometry(); if (geom instanceof Point) { Point p = (Point) geom; res.setLat(p.getCoordinates().getLatitude()); res.setLng(p.getCoordinates().getLongitude()); res.setDescription(feature.getProperties().get("label").toString());
private static POI toPoi(Feature feature) { LngLatAlt coordinates = ((Point)feature.getGeometry()).getCoordinates(); Map<String, String> osmTags = feature.getProperty("osmTags"); if (osmTags == null) osmTags = new HashMap<>(); String openingHours = osmTags.get("opening_hours"); if (openingHours != null) openingHours = openingHours.replaceAll("; *", "\n"); String website = feature.getProperty("website"); if (TextUtils.isEmpty(website)) website = osmTags.get("url"); return new POI(Integer.parseInt(feature.getProperty("id")), feature.getProperty("name"), feature.getProperty("notes"), website, osmTags.get("phone"), openingHours, coordinates.getLatitude(), coordinates.getLongitude()); }
@Test public void writes_the_GeoJSON_file() throws IOException { List<Feature> outputFeatures = IntStream.range(0, 1000).mapToObj(i -> buildFeature()).collect(Collectors.toList()); GeoJson.write(output, outputFeatures.stream()); FeatureCollection parsedFeatureCollection = new ObjectMapper().readValue(Files.newInputStream(output), FeatureCollection.class); List<Feature> actualFeatures = parsedFeatureCollection.getFeatures(); assertThat(actualFeatures, hasSize(1000)); assertThat(actualFeatures, allMatch(feature -> feature.getGeometry() instanceof Point)); assertThat(actualFeatures, allMatch(feature -> ((Point) feature.getGeometry()).getCoordinates() != null)); assertThat(actualFeatures, allMatch(feature -> feature.getProperties().containsKey("key") && feature.getProperty("key") != null)); assertThat(actualFeatures, allMatch(feature -> feature.getProperties().containsKey("field") && feature.getProperty("field").equals("some-field"))); assertThat(actualFeatures, allMatch(feature -> feature.getProperties().containsKey("empty") && feature.getProperty("empty").equals("no"))); assertThat(actualFeatures, allMatch(feature -> feature.getProperties().containsKey("valid") && feature.getProperty("valid").equals("yes"))); }
/** * Create a Bounds from a GeoServiceBounds. * * @param feature the bounding box feature * @return the Bounds */ default Bounds toBounds(final Feature feature) { if (feature == null) { return null; } final Polygon polygon = (Polygon) feature.getGeometry(); final List<List<LngLatAlt>> coordinates = polygon.getCoordinates(); if (coordinates == null || coordinates.isEmpty()) { return new Bounds(); } final List<LngLatAlt> list = coordinates.get(0); final Bounds bounds = toBounds(list.get(2), list.get(0)); return bounds; }
private List<EventProperty> parseGeometryField(Feature geoFeature) { List<EventProperty> eventProperties = new LinkedList<>(); if(geoFeature.getGeometry() instanceof Point) { Point point = (Point) geoFeature.getGeometry(); eventProperties.add(getEventPropertyGeoJson("longitude", point.getCoordinates().getLongitude(), SO.Longitude)); eventProperties.add(getEventPropertyGeoJson("latitude", point.getCoordinates().getLatitude(), SO.Latitude)); if (point.getCoordinates().hasAltitude()) { eventProperties.add(getEventPropertyGeoJson("altitude", point.getCoordinates().getAltitude(), SO.Altitude)); } } else if (geoFeature.getGeometry() instanceof LineString) { LineString lineString = (LineString) geoFeature.getGeometry(); eventProperties.add(JsonEventProperty.getEventProperty("coorindatesLineString", lineString.getCoordinates())); } else if (geoFeature.getGeometry() instanceof Polygon) { Polygon polygon = (Polygon) geoFeature.getGeometry(); eventProperties.add(JsonEventProperty.getEventProperty("coorindatesPolygon", polygon.getCoordinates())); } else if (geoFeature.getGeometry() instanceof MultiPoint) { MultiPoint multiPoint = (MultiPoint) geoFeature.getGeometry(); eventProperties.add(JsonEventProperty.getEventProperty("coorindatesMultiPoint", multiPoint.getCoordinates())); } else if (geoFeature.getGeometry() instanceof MultiLineString) { MultiLineString multiLineString = (MultiLineString) geoFeature.getGeometry(); eventProperties.add(JsonEventProperty.getEventProperty("coorindatesMultiLineString", multiLineString.getCoordinates())); } else if (geoFeature.getGeometry() instanceof MultiPolygon) { MultiPolygon multiPolygon = (MultiPolygon) geoFeature.getGeometry(); eventProperties.add(JsonEventProperty.getEventProperty("coorindatesMultiPolygon", multiPolygon.getCoordinates())); } else { logger.error("No geometry field found in geofeature: " + geoFeature.toString()); } return eventProperties; }
@SuppressWarnings("unchecked") protected static <V> V propertyOrDefault(Feature feature, String propertyName, V defaultValue) { return (feature.getProperty(propertyName) == null) ? defaultValue : (V)feature.getProperty(propertyName); } }
/** * Convert a Point and LocationType into a Feature. * * @param point the Point * @param locationType the LocationType * @return the Feature */ private static Feature createLocation(final Point point, final LocationType locationType) { final Feature location = new Feature(); location.setGeometry(point); location.setProperty("locationType", locationType); location.setId("location"); return location; }
private Feature buildFeature() { Feature feature = new Feature(); feature.setGeometry(new Point(Math.random() * 360 - 180, Math.random() * 180 - 90)); feature.setProperty("key", instanceIdSeq.getAndIncrement()); feature.setProperty("field", "some-field"); feature.setProperty("empty", "no"); feature.setProperty("valid", "yes"); return feature; } }
final LngLatAlt southwest, final LngLatAlt northeast) { final Feature feature = new Feature(); feature.setId(id); if (northeast == null || southwest == null) { throw new IllegalArgumentException( }; final Polygon polygon = new Polygon(); feature.setGeometry(polygon); polygon.setBbox(bbox); final List<LngLatAlt> elements = new ArrayList<>(5); elements.add(new LngLatAlt(bbox[SW_LNG], bbox[SW_LAT])); polygon.add(elements); feature.setBbox(bbox); return feature;
@Override public EventSchema getEventSchema(List<byte[]> oneEvent) { EventSchema resultSchema = new EventSchema(); Feature geoFeature = null; try { geoFeature = new ObjectMapper().readValue(oneEvent.get(0), Feature.class); } catch (IOException e) { logger.error(e.toString()); } for (Map.Entry<String, Object> entry : geoFeature.getProperties().entrySet()) { EventProperty p = JsonEventProperty.getEventProperty(entry.getKey(), entry.getValue()); resultSchema.addEventProperty(p); } List<EventProperty> eventProperties = parseGeometryField(geoFeature); eventProperties.forEach(eventProperty -> resultSchema.addEventProperty(eventProperty)); return resultSchema; }
/** * Build a bounding box feature. * In this case, it has a name but is empty. * * @param id the ID string * @return the Feature for the new bounds */ public static Feature createBounds(final String id) { final Feature feature = new Feature(); feature.setId(id); feature.setGeometry(new Polygon()); return feature; }
&& !geometry.getFeatures().isEmpty()) { final Feature fg = geometry.getFeatures().get(0); fg.setProperty("formattedAddress", formattedAddress); fg.setProperty("partialMatch", Boolean.valueOf(partialMatch)); fg.setProperty("placeId", placeId); if (postcodeLocalities == null) { fg.setProperty("postcodeLocalities", null); } else { fg.setProperty("postcodeLocalities", Arrays .copyOf(postcodeLocalities, postcodeLocalities.length)); fg.setProperty("types", types); fg.setProperty("addressComponents", addressComponents);
/** * @param geometry geometry object to fill in * @param location the location to fill it with */ private void populateLocation(final Geometry geometry, final Feature location) { if (location == null) { geometry.location = null; geometry.locationType = null; } else { geometry.location = toLatLng((Point) location.getGeometry()); geometry.locationType = toLocationType(location.getProperty("locationType")); } }
public String getAddressFor(NavigationPosition position) throws IOException { FeatureCollection collection = getResultFor("/reverse/?lon=" + position.getLongitude() + "&lat=" + position.getLatitude()); if (collection == null) return null; List<Feature> features = collection.getFeatures(); if (features.size() == 0) return null; Feature feature = features.get(0); GeoJsonObject geometry = feature.getGeometry(); if (!(geometry instanceof Point)) return null; return getDisplayName(feature); }
private List<EventProperty> parseGeometryField(Feature geoFeature) { List<EventProperty> eventProperties = new LinkedList<>(); if(geoFeature.getGeometry() instanceof Point) { Point point = (Point) geoFeature.getGeometry(); eventProperties.add(getEventPropertyGeoJson("longitude", point.getCoordinates().getLongitude(), SO.Longitude)); eventProperties.add(getEventPropertyGeoJson("latitude", point.getCoordinates().getLatitude(), SO.Latitude)); if (point.getCoordinates().hasAltitude()) { eventProperties.add(getEventPropertyGeoJson("altitude", point.getCoordinates().getAltitude(), SO.Altitude)); } } else if (geoFeature.getGeometry() instanceof LineString) { LineString lineString = (LineString) geoFeature.getGeometry(); eventProperties.add(JsonEventProperty.getEventProperty("coorindatesLineString", lineString.getCoordinates())); } else if (geoFeature.getGeometry() instanceof Polygon) { Polygon polygon = (Polygon) geoFeature.getGeometry(); eventProperties.add(JsonEventProperty.getEventProperty("coorindatesPolygon", polygon.getCoordinates())); } else if (geoFeature.getGeometry() instanceof MultiPoint) { MultiPoint multiPoint = (MultiPoint) geoFeature.getGeometry(); eventProperties.add(JsonEventProperty.getEventProperty("coorindatesMultiPoint", multiPoint.getCoordinates())); } else if (geoFeature.getGeometry() instanceof MultiLineString) { MultiLineString multiLineString = (MultiLineString) geoFeature.getGeometry(); eventProperties.add(JsonEventProperty.getEventProperty("coorindatesMultiLineString", multiLineString.getCoordinates())); } else if (geoFeature.getGeometry() instanceof MultiPolygon) { MultiPolygon multiPolygon = (MultiPolygon) geoFeature.getGeometry(); eventProperties.add(JsonEventProperty.getEventProperty("coorindatesMultiPolygon", multiPolygon.getCoordinates())); } else { logger.error("No geometry field found in geofeature: " + geoFeature.toString()); } return eventProperties; }
private String getProperty(Feature feature, String propertyName) { Object property = feature.getProperty(propertyName); return property != null ? trim(property.toString()) : ""; }
/** * Create a GeoJSON Point from a LatLng. * * @param latLng the LatLng * @param locationType the location type * @return the GeoServiceLatLng */ default Feature toLocationFeature(final LatLng latLng, final LocationType locationType) { if (latLng == null) { final Feature feature = new Feature(); feature.setProperty("locationType", locationType); feature.setId("location"); return feature; } final Point point = new Point(latLng.lng, latLng.lat); final Feature feature = new Feature(); feature.setGeometry(point); feature.setProperty("locationType", locationType); feature.setId("location"); return feature; } }
@Override public EventSchema getEventSchema(List<byte[]> oneEvent) { EventSchema resultSchema = new EventSchema(); Feature geoFeature = null; try { geoFeature = new ObjectMapper().readValue(oneEvent.get(0), Feature.class); } catch (IOException e) { logger.error(e.toString()); } for (Map.Entry<String, Object> entry : geoFeature.getProperties().entrySet()) { EventProperty p = JsonEventProperty.getEventProperty(entry.getKey(), entry.getValue()); resultSchema.addEventProperty(p); } List<EventProperty> eventProperties = parseGeometryField(geoFeature); eventProperties.forEach(eventProperty -> resultSchema.addEventProperty(eventProperty)); return resultSchema; }
private static GeoPlace toGeoPlace(Feature feature) { LngLatAlt coordinates = ((Point)feature.getGeometry()).getCoordinates(); return new GeoPlace(new GeoPoint(coordinates.getLatitude(), coordinates.getLongitude()), (String)feature.getProperty("name"), (String)feature.getProperty("near")); } }