Permalink
Browse files

added to GeoFeature the ability to turn itself into a "Thing", way ha…

…rder than it has any right to be, simple udf that calls that function
  • Loading branch information...
1 parent 38c5cd3 commit 25a62b80579cb4f76d7f6f6a84735aa59ba0d199 @thedatachef thedatachef committed Aug 3, 2011
Binary file not shown.
Binary file not shown.
View
@@ -62,9 +62,9 @@
<dependency>
<groupId>org.mapfish</groupId>
<artifactId>geo</artifactId>
- <version>1.2-SNAPSHOT</version>
+ <version>1.3-SNAPSHOT</version>
<scope>system</scope>
- <systemPath>${project.basedir}/lib/mapfish-geo-lib-1.2-SNAPSHOT.jar</systemPath>
+ <systemPath>${project.basedir}/lib/mapfish-geo-lib-1.3-SNAPSHOT.jar</systemPath>
</dependency>
@@ -35,6 +35,33 @@ public String serialize() {
return stringer.toString();
}
+ // If this looks disgusting to you, thats because it is.
+ public String toIcssThing() {
+ JSONStringer stringer = new JSONStringer();
+ MfGeoJSONWriter builder = new MfGeoJSONWriter(stringer);
+ try {
+ stringer.object();
+ toJSON(stringer);
+ if (getFeatureId() != null) {
+ // Add md5id
+ stringer.key("md5id").value(getFeatureId());
+ }
+
+ // add weird 'geo_geometry_type' key
+ stringer.key("geo_geometry_type");
+ stringer.value(geometry.getInternalGeometry().getGeometryType());
+
+ // Add coordinates
+ builder.encodeGeometryCoordinates(geometry.getInternalGeometry());
+
+ stringer.endObject();
+ } catch (Exception e) {
+ e.printStackTrace();
+ return null;
+ }
+ return stringer.toString();
+ }
+
public String getFeatureId() {
return id;
}
@@ -0,0 +1,56 @@
+package com.infochimps.hadoop.pig.geo;
+
+import java.io.IOException;
+import java.util.HashMap;
+
+import org.apache.pig.EvalFunc;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.data.DataBag;
+import org.apache.pig.data.TupleFactory;
+
+import org.mapfish.geo.MfGeo;
+import org.mapfish.geo.MfGeoFactory;
+import org.mapfish.geo.MfGeoJSONReader;
+import org.mapfish.geo.MfGeometry;
+import org.mapfish.geo.MfFeature;
+
+import com.vividsolutions.jts.geom.Polygon;
+import com.vividsolutions.jts.geom.Geometry;
+import com.vividsolutions.jts.geom.Point;
+import com.vividsolutions.jts.geom.Coordinate;
+
+import org.json.JSONException;
+import org.json.JSONObject;
+
+/**
+ Given a geoJSON feature, mangles it into an ICSS 'Thing'.
+ */
+public class GeoJSONToThing extends EvalFunc<String> {
+ private final MfGeoFactory mfFactory = new MfGeoFactory() {
+ public MfFeature createFeature(String id, MfGeometry geometry, JSONObject properties) {
+ return new GeoFeature(id, geometry, properties);
+ }
+ };
+
+ private final MfGeoJSONReader reader = new MfGeoJSONReader(mfFactory);
+
+ private static final String PERIOD = "\\.";
+ private static final String GEOJSON_ID = "id";
+ private static final String GEOJSON_TYPE = "type";
+ private static final String GEOJSON_FEATURE = "Feature";
+ private static final String GEOJSON_GEOM = "geometry";
+ private static final String GEOJSON_PROP = "properties";
+
+ public String exec(Tuple input) throws IOException {
+ if (input == null || input.size() < 1 || input.isNull(0))
+ return null;
+
+ String json = input.get(0).toString();
+ String result = null;
+ try {
+ GeoFeature feature = (GeoFeature)reader.decode(json);
+ result = feature.toIcssThing();
+ } catch (JSONException e) {}
+ return result;
+ }
+}

0 comments on commit 25a62b8

Please sign in to comment.