@@ -20,6 +20,7 @@ |
public class GeoJSONParser implements Parser { |
|
enum ShapeType {Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon, GeometryCollection}; |
+ public static String DEFAULT_CRS = "EPSG:4326"; // EPSG 4326 |
protected final Logger LOG = Logger.getLogger(getClass().getSimpleName()); |
|
JSONArray layersJSON; |
@@ -59,11 +60,11 @@ |
if (type.equalsIgnoreCase("FeatureCollection")) { |
JSONArray fts = layerJSON.getJSONArray("features"); |
for (int i = 0; i < fts.length(); i++) { |
- lyr.addFeature(parseFeature(fts.getJSONObject(i), lyr)); |
+ lyr.addFeature(parseFeature(fts.getJSONObject(i), layerJSON, lyr)); |
} |
} |
else if (type.equalsIgnoreCase("Feature")) { |
- lyr.addFeature(parseFeature(layerJSON, lyr)); |
+ lyr.addFeature(parseFeature(layerJSON, layerJSON, lyr)); |
} |
} catch (JSONException ex) { |
throw new csip.ServiceException(ex); |
@@ -71,10 +72,40 @@ |
return lyr; |
} |
|
- public Feature parseFeature(JSONObject featureJSON, Layer lyr) throws ServiceException { |
- return db.uploadFeature(featureJSON, lyr); |
+ public Feature parseFeature(JSONObject featureJSON, JSONObject layerJSON, Layer lyr) throws ServiceException { |
+ try { |
+ JSONObject crs = generateCRS(DEFAULT_CRS); |
+ if (hasCRS(featureJSON)) { |
+ crs = featureJSON.getJSONObject("crs"); |
+ } else if (hasCRS(layerJSON)) { |
+ crs = layerJSON.getJSONObject("crs"); |
+ } |
+ LOG.log(Level.INFO, "Chose CRS for layer " + lyr.toString() + ": " + crs.toString()); |
+ featureJSON.put("crs", crs); |
+ return db.uploadFeature(featureJSON, lyr); |
+ } catch (JSONException ex) { |
+ throw new ServiceException(ex); |
+ } |
} |
|
+ /** |
+ * Generates a geoJSON coordinate reference system object. |
+ * @param urn the OGC CRS URN string: http://geojson.org/geojson-spec.html#coordinate-reference-system-objects |
+ * @return |
+ */ |
+ public static JSONObject generateCRS(String urn) throws JSONException { |
+ JSONObject crs = new JSONObject(); |
+ JSONObject crsProps = new JSONObject(); |
+ crsProps.put("name", urn); |
+ crs.put("type", "name"); |
+ crs.put("properties", crsProps); |
+ return crs; |
+ } |
+ |
+ public static boolean hasCRS(JSONObject geoJSON) { |
+ return geoJSON.has("crs"); |
+ } |
+ |
public static ShapeType getShape(JSONObject layerJSON) throws JSONException { |
ShapeType shapeType = null; |
String type = layerJSON.getString("type"); |
@@ -56,7 +56,6 @@ |
Layer lyr = null; |
try { |
GeoJSONParser.ShapeType shapeType = GeoJSONParser.getShape(layerJSON); |
- int srid = GeoJSONParser.getSridInt(layerJSON); |
String schemaTable = SCHEMA + ".\"" + tableName + "\""; |
Statement stmt = conn.createStatement(); |
if (!exists(SCHEMA, tableName)) { |
@@ -66,11 +65,11 @@ |
|
sql = "SELECT AddGeometryColumn ('" + |
SCHEMA + "','" + tableName + "', 'the_geom', " + |
- String.valueOf(srid) + ", '" + shapeType + "', 2);"; |
+ "4326, '" + shapeType + "', 2);"; |
LOG.log(Level.INFO, sql); |
stmt.executeQuery(sql); |
} |
- lyr = new Layer(SCHEMA, tableName, layerJSON); |
+ lyr = new Layer(SCHEMA, tableName, this); |
|
stmt.close(); |
} catch (JSONException|SQLException ex) { |
@@ -83,8 +82,13 @@ |
public Feature uploadFeature(JSONObject featureJSON, Layer layer) throws csip.ServiceException { |
try { |
Statement stmt = conn.createStatement(); |
+ JSONObject geomJSON = featureJSON.getJSONObject("geometry"); |
+ if (!GeoJSONParser.hasCRS(geomJSON)) { |
+ geomJSON.put("crs", featureJSON.get("crs")); |
+ } |
|
- String sql = "SELECT ST_GeomFromGeoJSON('" + featureJSON.getJSONObject("geometry").toString() + "') AS geojson"; |
+ String sql = "SELECT ST_Transform(ST_GeomFromGeoJSON('" + |
+ geomJSON.toString() + "'), 4326) AS geojson"; |
LOG.log(Level.INFO, sql); |
ResultSet rs = stmt.executeQuery(sql); |
rs.next(); |
@@ -182,7 +186,7 @@ |
stmt.executeUpdate(sql); |
|
stmt.close(); |
- return new Layer(SCHEMA, table, null); |
+ return new Layer(SCHEMA, table, this); |
} catch (SQLException ex) { |
throw new csip.ServiceException(ex); |
} |
@@ -209,7 +213,7 @@ |
stmt.executeUpdate(sql); |
|
stmt.close(); |
- return new Layer(SCHEMA, table, null); |
+ return new Layer(SCHEMA, table, this); |
} catch (SQLException ex) { |
throw new csip.ServiceException(ex); |
} |
@@ -235,7 +239,7 @@ |
stmt.executeUpdate(sql); |
|
stmt.close(); |
- return new Layer(SCHEMA, table, null); |
+ return new Layer(SCHEMA, table, this); |
} catch (SQLException ex) { |
throw new csip.ServiceException(ex); |
} |
@@ -262,7 +266,7 @@ |
stmt.executeUpdate(sql); |
|
stmt.close(); |
- return new Layer(SCHEMA, table, null); |
+ return new Layer(SCHEMA, table, this); |
} catch (SQLException ex) { |
throw new csip.ServiceException(ex); |
} |
@@ -290,7 +294,7 @@ |
stmt.executeUpdate(sql); |
|
stmt.close(); |
- return new Layer(SCHEMA, table, null); |
+ return new Layer(SCHEMA, table, this); |
} catch (SQLException ex) { |
throw new csip.ServiceException(ex); |
} |
@@ -316,7 +320,7 @@ |
stmt.executeUpdate(sql); |
|
stmt.close(); |
- return new Layer(SCHEMA, table, null); |
+ return new Layer(SCHEMA, table, this); |
} catch (SQLException ex) { |
throw new csip.ServiceException(ex); |
} |
@@ -342,7 +346,7 @@ |
stmt.executeUpdate(sql); |
|
stmt.close(); |
- return new Layer(SCHEMA, table, null); |
+ return new Layer(SCHEMA, table, this); |
} catch (SQLException ex) { |
throw new csip.ServiceException(ex); |
} |
@@ -370,7 +374,7 @@ |
stmt.executeUpdate(sql); |
|
stmt.close(); |
- return new Layer(SCHEMA, table, null); |
+ return new Layer(SCHEMA, table, this); |
} catch (SQLException ex) { |
throw new csip.ServiceException(ex); |
} |