use of com.esri.ges.core.geoevent.GeoEvent in project reverse-geocoder-for-geoevent by Esri.
the class ReverseGeocoderProcessor method augmentGeoEventWithAddress.
private GeoEvent augmentGeoEventWithAddress(GeoEvent geoEvent, String addressJson) throws ConfigurationException, GeoEventDefinitionManagerException, FieldException, JSONException {
JSONObject jsonObj = new JSONObject(addressJson);
JSONObject addressJSONObject = jsonObj.getJSONObject("address");
GeoEvent newGeoEvent = null;
if (geoEventCreator != null) {
try {
GeoEventDefinition edIn = geoEvent.getGeoEventDefinition();
List<FieldDefinition> fds = new ArrayList<FieldDefinition>();
fds.add(new DefaultFieldDefinition("Address", FieldType.String));
fds.add(new DefaultFieldDefinition("Neighborhood", FieldType.String));
fds.add(new DefaultFieldDefinition("City", FieldType.String));
fds.add(new DefaultFieldDefinition("Subregion", FieldType.String));
fds.add(new DefaultFieldDefinition("Region", FieldType.String));
fds.add(new DefaultFieldDefinition("Postal", FieldType.String));
fds.add(new DefaultFieldDefinition("PostalExt", FieldType.String));
fds.add(new DefaultFieldDefinition("CountryCode", FieldType.String));
fds.add(new DefaultFieldDefinition("Match_addr", FieldType.String));
fds.add(new DefaultFieldDefinition("Loc_name", FieldType.String));
GeoEventDefinition edOut = edIn.augment(fds);
edOut.setName(newGeoEventDefinitionName);
edOut.setOwner(getId());
GeoEventDefinitionManager geoEventDefinitionManager = geoEventCreator.getGeoEventDefinitionManager();
// if (geoEventDefinitionManager.getGeoEventDefinition(newGeoEventDefinitionName) == null)
// geoEventDefinitionManager.addGeoEventDefinition(edOut);
// LOGGER.info("Checking for GED: " + edOut.getName());
String gedGuid = "";
try {
Collection<GeoEventDefinition> geoEventDefinitions = geoEventDefinitionManager.searchInRepositoryGeoEventDefinitionByName(newGeoEventDefinitionName);
// LOGGER.info("Found " + geoEventDefinitions.size() + " GEDs named " + edOut.getName());
if (geoEventDefinitions == null || geoEventDefinitions.size() == 0) {
// LOGGER.info("Didn't find it. Adding GED: " + edOut.getName());
geoEventDefinitionManager.addGeoEventDefinition(edOut);
} else
gedGuid = geoEventDefinitions.iterator().next().getGuid();
} catch (Exception ex) {
LOGGER.info("Encountered an error checking for this GED: " + edOut.getName() + ". Adding it anyway.");
geoEventDefinitionManager.addGeoEventDefinition(edOut);
gedGuid = edOut.getGuid();
}
List<Object> fieldValues = new ArrayList<Object>();
Object value;
for (int i = 0; i < edIn.getFieldDefinitions().size(); i++) {
value = geoEvent.getField(i);
fieldValues.add(value);
}
try {
fieldValues.add(addressJSONObject.get("Address"));
} catch (Exception ex) {
fieldValues.add("");
}
try {
fieldValues.add(addressJSONObject.get("Neighborhood"));
} catch (Exception ex) {
fieldValues.add("");
}
try {
fieldValues.add(addressJSONObject.get("City"));
} catch (Exception ex) {
fieldValues.add("");
}
try {
fieldValues.add(addressJSONObject.get("Subregion"));
} catch (Exception ex) {
fieldValues.add("");
}
try {
fieldValues.add(addressJSONObject.get("Region"));
} catch (Exception ex) {
fieldValues.add("");
}
try {
fieldValues.add(addressJSONObject.get("Postal"));
} catch (Exception ex) {
fieldValues.add("");
}
try {
fieldValues.add(addressJSONObject.get("PostalExt"));
} catch (Exception ex) {
fieldValues.add("");
}
try {
fieldValues.add(addressJSONObject.get("CountryCode"));
} catch (Exception ex) {
fieldValues.add("");
}
try {
fieldValues.add(addressJSONObject.get("Match_addr"));
} catch (Exception ex) {
fieldValues.add("");
}
try {
fieldValues.add(addressJSONObject.get("Loc_name"));
} catch (Exception ex) {
fieldValues.add("");
}
// LOGGER.info("Creating a Geoevent from GED: " + edOut.getName());
newGeoEvent = geoEventCreator.create(gedGuid);
// newGeoEvent = geoEventCreator.create(newGeoEventDefinitionName, definition.getUri().toString());
for (int i = 0; i < edOut.getFieldDefinitions().size(); i++) {
try {
newGeoEvent.setField(i, fieldValues.get(i));
} catch (Exception ex) {
}
}
newGeoEvent.setProperty(GeoEventPropertyName.TYPE, "event");
newGeoEvent.setProperty(GeoEventPropertyName.OWNER_ID, getId());
newGeoEvent.setProperty(GeoEventPropertyName.OWNER_URI, definition.getUri());
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return newGeoEvent;
}
use of com.esri.ges.core.geoevent.GeoEvent in project reverse-geocoder-for-geoevent by Esri.
the class ReverseGeocoderProcessor method processGeoEvent.
private GeoEvent processGeoEvent(GeoEvent geoEvent) throws MalformedURLException, JSONException, ConfigurationException, GeoEventDefinitionManagerException, FieldException {
if (geoEvent.getTrackId() == null || geoEvent.getGeometry() == null) {
LOGGER.warn("NULL_ERROR: TrackID and/or Geometry is NULL.");
return null;
}
Geometry geom = geoEvent.getGeometry().getGeometry();
if (geom.isEmpty())
return geoEvent;
if (!Geometry.isPoint(geom.getType().value()))
return geoEvent;
if (Geometry.isMultiVertex(geom.getType().value()))
return geoEvent;
Point point = (Point) geom;
double lon = point.getX();
double lat = point.getY();
int wkid = geoEvent.getGeometry().getSpatialReference().getID();
// fetch nearest street address (reverse geocode) via ArcGIS Online World GeoCode service
// The response format. Values: html | json | kmz
// The default response format is html.
agolSearchFormat = "json";
URL agolURL = new URL("http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/reverseGeocode?location=" + Double.toString(lon) + "," + Double.toString(lat) + "&distance=" + Integer.toString(agolSearchDistance) + "&outSR=" + Integer.toString(wkid) + "&f=" + agolSearchFormat);
String addressJson = getReverseGeocode(agolURL);
GeoEvent agolStreetAddress = augmentGeoEventWithAddress(geoEvent, addressJson);
return agolStreetAddress;
}
Aggregations