Skip to content

Commit

Permalink
added to GeoFeature the ability to turn itself into a "Thing", way ha…
Browse files Browse the repository at this point in the history
…rder than it has any right to be, simple udf that calls that function
  • Loading branch information
Jacob Perkins committed Aug 3, 2011
1 parent 38c5cd3 commit 25a62b8
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 2 deletions.
Binary file removed lib/mapfish-geo-lib-1.2-SNAPSHOT.jar
Binary file not shown.
Binary file added lib/mapfish-geo-lib-1.3-SNAPSHOT.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -62,9 +62,9 @@
<dependency> <dependency>
<groupId>org.mapfish</groupId> <groupId>org.mapfish</groupId>
<artifactId>geo</artifactId> <artifactId>geo</artifactId>
<version>1.2-SNAPSHOT</version> <version>1.3-SNAPSHOT</version>
<scope>system</scope> <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> </dependency>




Expand Down
27 changes: 27 additions & 0 deletions src/main/java/com/infochimps/hadoop/pig/geo/GeoFeature.java
Expand Up @@ -35,6 +35,33 @@ public String serialize() {
return stringer.toString(); 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() { public String getFeatureId() {
return id; return id;
} }
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/com/infochimps/hadoop/pig/geo/GeoJSONToThing.java
@@ -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.