Skip to content

Commit

Permalink
Merge pull request #68 from midas-isg/create_circle
Browse files Browse the repository at this point in the history
Creates circle, cache-config-key, uses ls_test db, api-doc updated
  • Loading branch information
espinoj authored Jul 12, 2017
2 parents bde5cec + 9fa51e5 commit cd8952f
Show file tree
Hide file tree
Showing 28 changed files with 800 additions and 480 deletions.
45 changes: 23 additions & 22 deletions app/controllers/AdministrativeUnitServices.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,54 +16,55 @@ public class AdministrativeUnitServices extends Controller {
static Status okJson(Object resultObject) {
return ok(Json.toJson(resultObject));
}

static void setResponseLocation(Long id) {
String uri = makeUri(id);
response().setHeader(LOCATION, uri);
}

static String makeUri(Long id) {
Request request = Context.current().request();
Logger.debug(""+ request.headers());
Logger.debug("" + request.headers());
String url = request.getHeader(ORIGIN) + request.path();
String result = url;
if (id != null)
result += "/" + id;
return result;
}

@BodyParser.Of(BodyParser.Json.class)
static FeatureCollection parseRequestAsFeatureCollection() throws Exception {
Request request = Context.current().request();
JsonNode requestJSON = null;
Logger.debug("\n");
if(request != null) {

Logger.debug("\n");
if (request != null) {
RequestBody requestBody = request.body();

// String requestBodyText = requestBody.toString();
//Logger.debug("Request [" + request.getHeader("Content-Type") + "], Length: " + requestBodyText.length() + "\n");
//Logger.debug("Request Body:\n" + requestBodyText + "\n");
//Logger.debug("Request.queryString():\n" + request.queryString() + "\n");
//Logger.debug("Request.headers().toString():\n" + request.headers().toString() + "\n");


// String requestBodyText = requestBody.toString();
// Logger.debug("Request [" + request.getHeader("Content-Type") +
// "], Length: " + requestBodyText.length() + "\n");
// Logger.debug("Request Body:\n" + requestBodyText + "\n");
// Logger.debug("Request.queryString():\n" + request.queryString() +
// "\n");
// Logger.debug("Request.headers().toString():\n" +
// request.headers().toString() + "\n");

requestJSON = requestBody.asJson();
if(requestJSON == null) {
if (requestJSON == null) {
throw new RuntimeException("Expecting JSON data");
}
else {
} else {
String type = requestJSON.findPath("type").textValue();
if(type == null) {

if (type == null) {
throw new RuntimeException("Missing parameter [type]");
}
}

return GeoJSONParser.parse(requestJSON);
}
else {
} else {
String message = "Request is null";
Logger.debug("\n" + message + "\n");
Logger.debug("\n" + message + "\n");
throw new RuntimeException(message);
}
}
Expand Down
3 changes: 0 additions & 3 deletions app/controllers/ApiDocument.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package controllers;

import play.Configuration;
import play.Play;
import play.db.jpa.Transactional;
import play.mvc.Controller;
import play.mvc.Result;

Expand Down
1 change: 0 additions & 1 deletion app/controllers/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import play.Configuration;
import play.Play;
import play.db.jpa.Transactional;
import play.mvc.Controller;
import play.mvc.Result;
import security.controllers.UserController;

Expand Down
66 changes: 43 additions & 23 deletions app/controllers/LocationServices.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ public class LocationServices extends Controller {
private static final String UNIQUE_VIOLATION = "23505";
private static final String findBulkEx = "find-bulk.json";
private static final String findBulkExV2 = "find-bulk-v2.json";
private static final String findBulkExBody = "Only \"queryTerm\" is required. See an example of body for vesrsion "
private static final String findBulkExBody = "Only \"queryTerm\" is required. See an example of body for version "
+ CURRENT_VERSION + " at "
+ "<a target='_blank' href='assets/examples/api/" + findBulkExV2 + "'>" + findBulkExV2 + "</a></br>"
+ "Vesrion " + OLD_VERSION + " body example: "
+ "Version " + OLD_VERSION + " body example: "
+ "<a target='_blank' href='assets/examples/api/" + findBulkEx + "'>" + findBulkEx + "</a> ";;
private static final String findByTermEx = "find-by-term.json";
private static final String findByTermExV2 = "find-by-term-v2.json";
private static final String findByTermExBody = "Only \"queryTerm\" is required. See a request example for vesrsion "
private static final String findByTermExBody = "Only \"queryTerm\" is required. See a request example for version "
+ CURRENT_VERSION + " at "
+ "<a target='_blank' href='assets/examples/api/" + findByTermExV2 + "'>" + findByTermExV2 + "</a></br>"
+ "Vesrion " + OLD_VERSION + " request example: "
+ "Version " + OLD_VERSION + " request example: "
+ "<a target='_blank' href='assets/examples/api/" + findByTermEx + "'>" + findByTermEx + "</a> ";
private static final String findByFilterEx = "find-by-filter.json";
private static final String findByFilterExBody = "Returns locations matched by filters. </br>"
Expand All @@ -73,6 +73,8 @@ public class LocationServices extends Controller {
private static final String findbyGeomEx = "AuMaridiTown.geojson";
private static final String findbyGeomExBody = "See an example of body at "
+ "<a target='_blank' href='assets/examples/api/" + findbyGeomEx + "'>" + findbyGeomEx + "</a> ";
private static final String circleExample = "circle.json";
private static final String featureFields = "featureFields.txt";
private static final String superTypeAPI = "/api/super-types";
private static final String locationTypeAPI = "/api/location-types";

Expand Down Expand Up @@ -116,14 +118,20 @@ public Result locations(
String format,

@ApiParam(
value = "Includes only the given fields in feature objects",
value = "Comma separated values</br>"
+ "Includes only the given fields in the response feature</br>"
+ "See "
+ "<a target='_blank' href='assets/examples/api/" + featureFields + "'>possible values</a> ",
required = false
)
@QueryParam("_onlyFeatureFields")
String onlyFeatureFields,

@ApiParam(
value = "Excludes the given fields from feature objects",
value = "Comma separated values</br>"
+ "Excludes the given fields from the response feature</br>"
+ "See "
+ "<a target='_blank' href='assets/examples/api/" + featureFields + "'>possible values</a> ",
required = false
)
@QueryParam("_excludedFeatureFields")
Expand Down Expand Up @@ -301,16 +309,16 @@ public Result filterByTerm(
nickname = "Find",
value = "Finds locations by name, other-names, or code",
notes = "<p>Receives a single query as shown in the example.</p>"
+ "<b>in version 1:</b> "
+ "<b>In version 1:</b> "
+ "<ul> "
+ "<li>response is not a valid geoJSON (<i>'geometry'</i> property is removed from FeatureCollection response).</li> "
+ "<li><i>\"properties.children\"</i> is excluded from features.</li> "
+ "</ul> "
+ "<b>as of version 2:</b> "
+ "<b>As of version 2:</b> "
+ "<ul> "
+ "<li>use <i>\"_onlyFeatureFields\"</i> or <i>\"_excludedFeatureFields\"</i> for filtering fields.</li>"
+ "<li>request filter-key names changed: 'includeOnly' -> <i>'onlyFeatureFields'</i> & 'exclude' -> <i>'excludedFeatureFields'</i>.</li> "
+ "<li>request filter syntax changed. Refer to <a href='assets/examples/api/" + findByTermExV2 + "'>" + findByTermExV2 + "</a></li> "
+ "<li>use <i>\"onlyFeatureFields\"</i> or <i>\"excludedFeatureFields\"</i> for filtering fields.</li> See "
+ "<a target='_blank' href='assets/examples/api/" + featureFields + "'>here</a> for possible values</li>. "
+ "<li>request filter syntax changed. See <a href='assets/examples/api/" + findByTermExV2 + "'>" + findByTermExV2 + "</a></li> "
+ "<li>\"verbose\" option is not suppoerted. Use <i>\"_onlyFeatureFields\":\"properties.gid\"</i> for non-verbose results.</li> "
+ "<li>the default for search logic is <i>\"logic\":\"OR\"</i>. For conjuction between query-terms use <i>\"logic\":\"AND\"</i></li> "
+ "</ul>",
Expand Down Expand Up @@ -580,9 +588,10 @@ public Result findByLocationPoint(double lat, double lon, boolean verbose) {
httpMethod = "POST",
nickname = "createLocation",
value = "Creates a location",
notes = "This endpoint creates a location using submitted GeoJSON FeatureCollection object "
+ "in body and return the URI via the 'Location' Header in the response. "
+ "Currently, no content returns in the body. ",
notes = "<p>This endpoint creates a location using submitted GeoJSON FeatureCollection object "
+ "in body (with an exception of a 'circle' which is not a GeoJSON) and returns the URI via the 'Location' Header in the response. "
+ "Currently, no content is returned in the body. </p>"
+ "<p>Circle is created using ST_Buffer(geography g1, float radius_of_buffer_in_meters) in postGIS 2.1.5.</p>",
response = Void.class
)
@ApiResponses(value = {
Expand All @@ -594,7 +603,12 @@ public Result findByLocationPoint(double lat, double lon, boolean verbose) {
})
@ApiImplicitParams( {
@ApiImplicitParam(
value = "GeoJSON FeatureCollection",
value = "<p>FeatureCollection</p>"
+ "<p>See examples:</p>"
+ "<p><a target='_blank' href='assets/examples/api/"
+ findbyGeomEx + "'>MuliPolygon</a></p>"
+ "<p><a target='_blank' href='assets/examples/api/"
+ circleExample + "'>Circle</a></p>",
required = true,
dataType = "models.geo.FeatureCollection",
paramType = "body"
Expand Down Expand Up @@ -814,7 +828,7 @@ public static Long delete(long gid) {

public static Result findByFeatureCollection(FeatureCollection fc, Long superTypeId, Long typeId,
boolean verbose) {
FeatureGeometry geometry = GeoJsonRule.asFetureGeometry(fc);
FeatureGeometry geometry = GeoJsonRule.asFeatureGeometry(fc);
String geo = Json.toJson(geometry).toString();
List<BigInteger> gids = GeometryRule.findGidsByGeometry(geo, superTypeId, typeId);
Map<String, Object> properties = new HashMap<>();
Expand Down Expand Up @@ -863,18 +877,24 @@ public Result findByTypeId(
Long typeId,

@ApiParam(
value = "Includes only the given fields in feature objects (as of version 2)",
required = false
value = "Comma separated values</br>"
+ "Includes only the given fields in the response feature <b>(as of version 2)</b></br>"
+ "See "
+ "<a target='_blank' href='assets/examples/api/" + featureFields + "'>possible values</a> ",
required = false
)
@QueryParam("_onlyFeatureFields")
String onlyFeatureFields,
String onlyFeatureFields,

@ApiParam(
value = "Excludes the given fields from feature objects (as of version 2)",
required = false
value = "Comma separated values</br>"
+ "Excludes the given fields from the response feature <b>(as of version 2)</b></br>"
+ "See "
+ "<a target='_blank' href='assets/examples/api/" + featureFields + "'>possible values</a> ",
required = false
)
@QueryParam("_excludedFeatureFields")
String excludedFeatureFields,
String excludedFeatureFields,

@ApiParam(
value = "Maximum number of locations to return (as of version 2)",
Expand Down
1 change: 0 additions & 1 deletion app/controllers/Test.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package controllers;

import play.mvc.Controller;
import play.mvc.Result;
import play.twirl.api.Html;
import security.controllers.AdminController;
Expand Down
47 changes: 41 additions & 6 deletions app/dao/GeometryDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@
import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.postgresql.util.PSQLException;

import com.vividsolutions.jts.geom.Geometry;

import play.Logger;
import play.db.jpa.JPA;
import dao.entities.LocationGeometry;
import models.exceptions.PostgreSQLException;

public class GeometryDao {

Expand All @@ -23,7 +28,6 @@ public LocationGeometry read(long gid, Class<LocationGeometry> geometryClass) {

public LocationGeometry read(EntityManager em, long gid,
Class<LocationGeometry> geometryClass) {
//Logger.debug("Find " + geometry.getSimpleName() + " where gid=" + gid);
return em.find(geometryClass, gid);
}

Expand All @@ -33,7 +37,7 @@ public LocationGeometry simplify(long gid, Double tolerance) {
String q = "select 0 as clazz_, 0 as gid, area, update_date, "
+ " ST_Simplify(multipolygon,?2) multipolygon, "
+ " rep_point "
+ " from location_geometry "
+ " from {h-schema}location_geometry "
+ " where gid=?1";
//@formatter:on
Query query = em.createNativeQuery(q, LocationGeometry.class);
Expand All @@ -57,7 +61,7 @@ public Long delete(LocationGeometry lg) {

public String readAsKml(long gid) {
EntityManager em = JPA.em();
String query = "select ST_AsKML(multipolygon) from location_geometry"
String query = "select ST_AsKML(multipolygon) from {h-schema}location_geometry"
+ " where gid = " + gid;
Object singleResult = em.createNativeQuery(query).getSingleResult();
return singleResult.toString();
Expand All @@ -69,7 +73,7 @@ public List<BigInteger> findGidsByPoint(double latitude, double longitude) {
String point = "ST_MakePoint(" + longitude + ", " + latitude +")";
String geometry = "ST_SetSRID("+ point+", "+ LocationDao.SRID + ")";
String q = "SELECT gid "
+ " FROM location_geometry "
+ " FROM {h-schema}location_geometry "
+ " WHERE ST_Contains(multipolygon, " + geometry + ")"
+ " ORDER BY ST_Area(multipolygon);";
//@formatter:on
Expand All @@ -84,7 +88,7 @@ public int numGeometriesAfterSimplified(long gid, Double tolerance) {
EntityManager em = JPA.em();
//@formatter:off
String q = "select ST_numGeometries(ST_Simplify(multipolygon,?2)) "
+ " from location_geometry where gid=?1";
+ " from {h-schema}location_geometry where gid=?1";
//@formatter:on
Query query = em.createNativeQuery(q);
query.setParameter(1, gid);
Expand All @@ -108,7 +112,7 @@ private String toQuery(String geojsonGeometry, Long superTypeId, Long typeId) {
String q =
"select a.gid "
+ "from "
+ " location_geometry a, location l, location_type t, "
+ " {h-schema}location_geometry a, {h-schema}location l, {h-schema}location_type t, "
+ " ST_SetSRID(ST_GeomFromGeoJSON('" + geojsonGeometry + "'), 4326) as b "
+ "where "
+ " l.gid = a.gid and l.location_type_id = t.id and "
Expand All @@ -117,4 +121,35 @@ private String toQuery(String geojsonGeometry, Long superTypeId, Long typeId) {
;
return q;
}

public static Geometry toBufferGeometry(double x, double y, double radius) {
EntityManager em = JPA.em();
String q = "SELECT 0 as clazz_, 0 as gid, 0 as area, CURRENT_DATE as update_date, "
+ " ST_Buffer(Geography((ST_MakePoint(?1, ?2))), ?3)\\:\\:geometry as multipolygon, "
+ " ST_MakePoint(?1, ?2) as rep_point ";

Query query = em.createNativeQuery(q, LocationGeometry.class);
query.setParameter(1, x);
query.setParameter(2, y);
query.setParameter(3, radius);
LocationGeometry lg = (LocationGeometry) query.getSingleResult();
Geometry geometry = lg.getShapeGeom();
return geometry;
}

public void create(LocationGeometry geometry, EntityManager em) {
try {
em.persist(geometry);
if (geometry.getCircleGeometry() != null) {
em.persist(geometry.getCircleGeometry());
}
em.flush();
} catch (Exception e) {
PSQLException pe = PostgreSQLException.toPSQLException(e);
if (pe != null) {
throw new PostgreSQLException(pe, pe.getSQLState());
} else
throw new RuntimeException(e);
}
}
}
Loading

0 comments on commit cd8952f

Please sign in to comment.