|
| 1 | +/* |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.appengine; |
| 18 | + |
| 19 | +import com.google.appengine.api.datastore.DatastoreService; |
| 20 | +import com.google.appengine.api.datastore.DatastoreServiceFactory; |
| 21 | +import com.google.appengine.api.datastore.Entity; |
| 22 | +import com.google.appengine.api.datastore.FetchOptions; |
| 23 | +import com.google.appengine.api.datastore.GeoPt; |
| 24 | +import com.google.appengine.api.datastore.Query; |
| 25 | +import com.google.appengine.api.datastore.Query.CompositeFilterOperator; |
| 26 | +import com.google.appengine.api.datastore.Query.Filter; |
| 27 | +import com.google.appengine.api.datastore.Query.FilterOperator; |
| 28 | +import com.google.appengine.api.datastore.Query.FilterPredicate; |
| 29 | +import com.google.appengine.api.datastore.Query.GeoRegion.Circle; |
| 30 | +import com.google.appengine.api.datastore.Query.GeoRegion.Rectangle; |
| 31 | +import com.google.appengine.api.datastore.Query.StContainsFilter; |
| 32 | + |
| 33 | +import java.io.IOException; |
| 34 | +import java.io.PrintWriter; |
| 35 | +import java.util.List; |
| 36 | + |
| 37 | +import javax.servlet.ServletException; |
| 38 | +import javax.servlet.http.HttpServlet; |
| 39 | +import javax.servlet.http.HttpServletRequest; |
| 40 | +import javax.servlet.http.HttpServletResponse; |
| 41 | + |
| 42 | +/** |
| 43 | + * A servlet to demonstrate the use of Cloud Datastore geospatial queries. |
| 44 | + */ |
| 45 | +public class GeoServlet extends HttpServlet { |
| 46 | + static final String BRAND_PROPERTY = "brand"; |
| 47 | + static final String LOCATION_PROPERTY = "location"; |
| 48 | + |
| 49 | + static final String BRAND_PARAMETER = "brand"; |
| 50 | + static final String LATITUDE_PARAMETER = "lat"; |
| 51 | + static final String LONGITUDE_PARAMETER = "lon"; |
| 52 | + static final String RADIUS_PARAMETER = "r"; |
| 53 | + |
| 54 | + static final String BRAND_DEFAULT = "Ocean Ave Shell"; |
| 55 | + static final String LATITUDE_DEFAULT = "37.7895873"; |
| 56 | + static final String LONGITUDE_DEFAULT = "-122.3917317"; |
| 57 | + static final String RADIUS_DEFAULT = "1000.0"; |
| 58 | + |
| 59 | + // Number of meters (approximately) in 1 degree of latitude. |
| 60 | + // http://gis.stackexchange.com/a/2964 |
| 61 | + private static final double DEGREE_METERS = 111111.0; |
| 62 | + |
| 63 | + private final DatastoreService datastore; |
| 64 | + |
| 65 | + public GeoServlet() { |
| 66 | + datastore = DatastoreServiceFactory.getDatastoreService(); |
| 67 | + } |
| 68 | + |
| 69 | + private static String getParameterOrDefault( |
| 70 | + HttpServletRequest req, String parameter, String defaultValue) { |
| 71 | + String value = req.getParameter(parameter); |
| 72 | + if (value == null || value.isEmpty()) { |
| 73 | + value = defaultValue; |
| 74 | + } |
| 75 | + return value; |
| 76 | + } |
| 77 | + |
| 78 | + private static GeoPt getOffsetPoint( |
| 79 | + GeoPt original, double latOffsetMeters, double lonOffsetMeters) { |
| 80 | + // Approximate the number of degrees to offset by meters. |
| 81 | + // http://gis.stackexchange.com/a/2964 |
| 82 | + // How long (approximately) is one degree of longitude? |
| 83 | + double lonDegreeMeters = DEGREE_METERS * Math.cos(original.getLatitude()); |
| 84 | + return new GeoPt( |
| 85 | + (float) (original.getLatitude() + latOffsetMeters / DEGREE_METERS), |
| 86 | + // This may cause errors if given points near the north or south pole. |
| 87 | + (float) (original.getLongitude() + lonOffsetMeters / lonDegreeMeters)); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void doGet(HttpServletRequest req, HttpServletResponse resp) |
| 92 | + throws IOException, ServletException { |
| 93 | + resp.setContentType("text/plain"); |
| 94 | + resp.setCharacterEncoding("UTF-8"); |
| 95 | + PrintWriter out = resp.getWriter(); |
| 96 | + |
| 97 | + String brand = getParameterOrDefault(req, BRAND_PARAMETER, BRAND_DEFAULT); |
| 98 | + String latStr = getParameterOrDefault(req, LATITUDE_PARAMETER, LATITUDE_DEFAULT); |
| 99 | + String lonStr = getParameterOrDefault(req, LONGITUDE_PARAMETER, LONGITUDE_DEFAULT); |
| 100 | + String radiusStr = getParameterOrDefault(req, RADIUS_PARAMETER, RADIUS_DEFAULT); |
| 101 | + |
| 102 | + float latitude; |
| 103 | + float longitude; |
| 104 | + double r; |
| 105 | + try { |
| 106 | + latitude = Float.parseFloat(latStr); |
| 107 | + longitude = Float.parseFloat(lonStr); |
| 108 | + r = Double.parseDouble(radiusStr); |
| 109 | + } catch (IllegalArgumentException e) { |
| 110 | + resp.sendError( |
| 111 | + HttpServletResponse.SC_BAD_REQUEST, String.format("Got bad value: %s", e.getMessage())); |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + // Get lat/lon for rectangle. |
| 116 | + GeoPt c = new GeoPt(latitude, longitude); |
| 117 | + GeoPt ne = getOffsetPoint(c, r, r); |
| 118 | + float neLat = ne.getLatitude(); |
| 119 | + float neLon = ne.getLongitude(); |
| 120 | + GeoPt sw = getOffsetPoint(c, -1 * r, -1 * r); |
| 121 | + float swLat = sw.getLatitude(); |
| 122 | + float swLon = sw.getLongitude(); |
| 123 | + |
| 124 | + // [START geospatial_stcontainsfilter_examples] |
| 125 | + // Testing for containment within a circle |
| 126 | + GeoPt center = new GeoPt(latitude, longitude); |
| 127 | + double radius = r; // Value is in meters. |
| 128 | + Filter f1 = new StContainsFilter("location", new Circle(center, radius)); |
| 129 | + Query q1 = new Query("GasStation").setFilter(f1); |
| 130 | + |
| 131 | + // Testing for containment within a rectangle |
| 132 | + GeoPt southwest = new GeoPt(swLat, swLon); |
| 133 | + GeoPt northeast = new GeoPt(neLat, neLon); |
| 134 | + Filter f2 = new StContainsFilter("location", new Rectangle(southwest, northeast)); |
| 135 | + Query q2 = new Query("GasStation").setFilter(f2); |
| 136 | + // [END geospatial_stcontainsfilter_examples] |
| 137 | + |
| 138 | + List<Entity> circleResults = datastore.prepare(q1).asList(FetchOptions.Builder.withDefaults()); |
| 139 | + out.printf("Got %d stations in %f meter radius circle.\n", circleResults.size(), radius); |
| 140 | + printStations(out, circleResults); |
| 141 | + out.println(); |
| 142 | + |
| 143 | + List<Entity> rectResults = datastore.prepare(q2).asList(FetchOptions.Builder.withDefaults()); |
| 144 | + out.printf("Got %d stations in rectangle.\n", rectResults.size()); |
| 145 | + printStations(out, rectResults); |
| 146 | + out.println(); |
| 147 | + |
| 148 | + List<Entity> brandResults = getStationsWithBrand(center, radius, brand); |
| 149 | + out.printf("Got %d stations in circle with brand %s.\n", brandResults.size(), brand); |
| 150 | + printStations(out, brandResults); |
| 151 | + out.println(); |
| 152 | + } |
| 153 | + |
| 154 | + private void printStations(PrintWriter out, List<Entity> stations) { |
| 155 | + for (Entity station : stations) { |
| 156 | + GeoPt location = (GeoPt) station.getProperty(LOCATION_PROPERTY); |
| 157 | + out.printf( |
| 158 | + "%s: @%f, %f\n", |
| 159 | + (String) station.getProperty(BRAND_PROPERTY), |
| 160 | + location.getLatitude(), |
| 161 | + location.getLongitude()); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + private List<Entity> getStationsWithBrand(GeoPt center, double radius, String value) { |
| 166 | + // [START geospatial_containment_and_equality_combination] |
| 167 | + Filter f = |
| 168 | + CompositeFilterOperator.and( |
| 169 | + new StContainsFilter("location", new Circle(center, radius)), |
| 170 | + new FilterPredicate("brand", FilterOperator.EQUAL, value)); |
| 171 | + // [END geospatial_containment_and_equality_combination] |
| 172 | + Query q = new Query("GasStation").setFilter(f); |
| 173 | + return datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()); |
| 174 | + } |
| 175 | +} |
0 commit comments