This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.google.appengine.geo.fencing; | |
/** The object model for the data we are sending through endpoints */ | |
public class MyFence { | |
private long id = -1; | |
public String name; | |
public String entityGroup; | |
public String description; | |
public double[][] vertices; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Get all fences of group from DataStore. | |
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); | |
Key fenceKey = KeyFactory.createKey("Geofence", group); | |
Query query = new Query("Fence", fenceKey).addSort("id", Query.SortDirection.DESCENDING); | |
List < Entity > fencesFromStore = datastore.prepare(query).asList(FetchOptions.Builder.withDefaults()); | |
if (!fencesFromStore.isEmpty()) { | |
//Create STRTree-Index. | |
STRtree index = new STRtree(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Build the index. | |
index.build(); | |
//Write the index to Memcache. | |
MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService(); | |
//Last param is expiration date. Set to null to keep it in Memcache forever. | |
syncCache.put(group, index, null); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ApiMethod(name = "add", httpMethod = "post", path = "add") | |
public MyFence addFence(@Named("group") String group, @Named("index") boolean buildIndex, MyFence fence) { | |
//Get the last fences' id. | |
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); | |
Key fenceKey = KeyFactory.createKey("Geofence", group); | |
long nextId; | |
if (fence.getId() != -1) { | |
nextId = fence.getId(); | |
} else { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ApiMethod(name = "getById", httpMethod = "get", path = "getById") | |
public ArrayList < MyFence > getFenceById(@Named("group") String group, @Named("id") long id) { | |
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); | |
Key fenceKey = KeyFactory.createKey("Geofence", group); | |
Filter propertyFilter = new FilterPredicate("id", FilterOperator.EQUAL, id); | |
Query query = new Query("Fence", fenceKey).setFilter(propertyFilter); | |
Entity fenceFromStore = datastore.prepare(query).asSingleEntity(); | |
ArrayList < MyFence > fences = new ArrayList < MyFence > (); | |
if (fenceFromStore != null) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ApiMethod(name = "list", httpMethod = "get", path = "list") | |
public ArrayList < MyFence > listFences(@Named("group") String group) { | |
ArrayList < MyFence > fences = new ArrayList < MyFence > (); | |
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); | |
Key fenceKey = KeyFactory.createKey("Geofence", group); | |
Query query = new Query("Fence", fenceKey).addSort("id", Query.SortDirection.DESCENDING); | |
List < Entity > fencesFromStore = datastore.prepare(query).asList(FetchOptions.Builder.withDefaults()); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ApiMethod(name = "polygon", httpMethod = "post", path = "polygon") | |
public ArrayList < MyFence > queryPolygon(@Named("group") String group, MyPolyLine polyline) { | |
ArrayList < MyFence > fences = new ArrayList < MyFence > (); | |
//Get index from Memcache | |
MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService(); | |
STRtree index = (STRtree) syncCache.get(group); // read from cache | |
if (index != null) { | |
//Create coordinate array. | |
double[][] points = polyline.getCoordinates(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ApiMethod(name = "polyline", httpMethod = "post", path = "polyline") | |
public ArrayList < MyFence > queryPolyLine(@Named("group") String group, MyPolyLine polyline) { | |
ArrayList < MyFence > fences = new ArrayList < MyFence > (); | |
//Get the index from Memcache. | |
MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService(); | |
GeometryFactory gf = new GeometryFactory(); | |
STRtree index = (STRtree) syncCache.get(group); // read from cache | |
if (index != null) { | |
//Create coordinate array. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ApiMethod(name = "point", httpMethod = "get", path = "point") | |
public ArrayList < MyFence > queryPoint(@Named("group") String group, @Named("lng") double lng, @Named("lat") double lat) { | |
ArrayList < MyFence > fences = new ArrayList < MyFence > (); | |
//Get the Index from Memcache. | |
MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService(); | |
GeometryFactory gf = new GeometryFactory(); | |
STRtree index = (STRtree) syncCache.get(group); // read from cache | |
if (index != null) { | |
Coordinate coord = new Coordinate(lng, lat); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
pickup_polyId, | |
SUM(tip_amount)/COUNT(*) AS average_tip, | |
COUNT(*) AS no_of_trips | |
FROM | |
[nyctaximap:dataflow.nyc_output_join_fare_distinct] | |
WHERE | |
tip_amount!=0 | |
GROUP BY | |
pickup_polyId |
OlderNewer