com.google.appengine.api.search.GeoPoint Java Examples
The following examples show how to use
com.google.appengine.api.search.GeoPoint.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PlacesHelper.java From MobileShoppingAssistant-sample with Apache License 2.0 | 6 votes |
/** * Builds a new Place document to insert in the Places index. * @param placeId the identifier of the place in the database. * @param placeName the name of the place. * @param placeAddress the address of the place. * @param location the GPS location of the place, as a GeoPt. * @return the Place document created. */ public static Document buildDocument( final Long placeId, final String placeName, final String placeAddress, final GeoPt location) { GeoPoint geoPoint = new GeoPoint(location.getLatitude(), location.getLongitude()); Document.Builder builder = Document.newBuilder() .addField(Field.newBuilder().setName("id") .setText(placeId.toString())) .addField(Field.newBuilder().setName("name").setText(placeName)) .addField(Field.newBuilder().setName("address") .setText(placeAddress)) .addField(Field.newBuilder().setName("place_location") .setGeoPoint(geoPoint)); // geo-location doesn't work under dev_server, so let's add another // field to use for retrieving documents if (environment.value() == Development) { builder.addField(Field.newBuilder().setName("value").setNumber(1)); } return builder.build(); }
Example #2
Source File: PlacesHelper.java From solutions-mobile-shopping-assistant-backend-java with Apache License 2.0 | 6 votes |
static Document buildDocument( String placeId, String placeName, String placeAddress, GeoPt location) { GeoPoint geoPoint = new GeoPoint(location.getLatitude(), location.getLongitude()); Document.Builder builder = Document.newBuilder() .addField(Field.newBuilder().setName("id").setText(placeId)) .addField(Field.newBuilder().setName("name").setText(placeName)) .addField(Field.newBuilder().setName("address").setText(placeAddress)) .addField(Field.newBuilder().setName("place_location").setGeoPoint(geoPoint)); // geo-location doesn't work under dev_server, so let's add another // field to use for retrieving documents if (environment.value() == Development) { builder.addField(Field.newBuilder().setName("value").setNumber(1)); } Document place = builder.build(); return place; }
Example #3
Source File: SearchTestBase.java From appengine-tck with Apache License 2.0 | 5 votes |
protected void addDocs(Index index, int docCount) throws ParseException, InterruptedException { if (searchDocs(index, "", 0).getNumberFound() == 0) { List<Document> documents = new ArrayList<>(); Calendar cal = Calendar.getInstance(); DateFormat dfDate = new SimpleDateFormat("yyyy,M,d"); for (int i = 0; i < docCount; i++) { Builder docBuilder = Document.newBuilder(); // two text field with different locale docBuilder.addField(Field.newBuilder().setName("textfield").setText("text with num " + i)); Field field = Field.newBuilder().setName("textfield").setText("C'est la vie " + i).setLocale(Locale.FRENCH).build(); docBuilder.addField(field); docBuilder.addField(Field.newBuilder().setName("numfield").setNumber(i)); String dateVal = "" + cal.get(Calendar.YEAR) + ","; dateVal += cal.get(Calendar.MONTH) + ","; int day = cal.get(Calendar.DATE) + i; dateVal += day; docBuilder.addField(Field.newBuilder().setName("datefield").setDate(dfDate.parse(dateVal))); docBuilder.addField(Field.newBuilder().setName("htmlfield").setHTML("<B>html</B> " + i)); docBuilder.addField(Field.newBuilder().setName("atomfield").setAtom("atom" + i + ".com")); GeoPoint geoPoint = new GeoPoint((double) i, (double) (100 + i)); docBuilder.addField(Field.newBuilder().setName("geofield").setGeoPoint(geoPoint)); // two field in same name and with different field type docBuilder.addField(Field.newBuilder().setName("mixfield").setText("text and number mix field")); docBuilder.addField(Field.newBuilder().setName("mixfield").setNumber(987)); docBuilder.setId("selfid" + i); // only doc(id="selfid0") has "cn" locale, others have "en" locale if (i == 0) { docBuilder.setLocale(new Locale("cn")); } else { docBuilder.setLocale(new Locale("en")); } documents.add(docBuilder.build()); } index.put(documents); sync(); } }
Example #4
Source File: PlacesHelper.java From MobileShoppingAssistant-sample with Apache License 2.0 | 4 votes |
/** * Returns the nearest places to the location of the user. * @param location the location of the user. * @param distanceInMeters the maximum distance to the user. * @param resultCount the maximum number of places returned. * @return List of up to resultCount places in the datastore ordered by * the distance to the location parameter and less than * distanceInMeters meters to the location parameter. */ public static List<PlaceInfo> getPlaces(final GeoPt location, final long distanceInMeters, final int resultCount) { // Optional: use memcache String geoPoint = "geopoint(" + location.getLatitude() + ", " + location .getLongitude() + ")"; String locExpr = "distance(place_location, " + geoPoint + ")"; // Build the SortOptions with 2 sort keys SortOptions sortOptions = SortOptions.newBuilder() .addSortExpression(SortExpression.newBuilder() .setExpression(locExpr) .setDirection(SortExpression.SortDirection.ASCENDING) .setDefaultValueNumeric(distanceInMeters + 1)) .setLimit(resultCount) .build(); // Build the QueryOptions QueryOptions options = QueryOptions.newBuilder() .setSortOptions(sortOptions) .build(); // Query string String searchQuery = "distance(place_location, " + geoPoint + ") < " + distanceInMeters; Query query = Query.newBuilder().setOptions(options).build(searchQuery); Results<ScoredDocument> results = getIndex().search(query); if (results.getNumberFound() == 0) { // geo-location doesn't work under dev_server if (environment.value() == Development) { // return all documents results = getIndex().search("value > 0"); } } List<PlaceInfo> places = new ArrayList<>(); for (ScoredDocument document : results) { if (places.size() >= resultCount) { break; } GeoPoint p = document.getOnlyField("place_location").getGeoPoint(); PlaceInfo place = new PlaceInfo(); place.setPlaceId(Long.valueOf(document.getOnlyField("id") .getText())); place.setName(document.getOnlyField("name").getText()); place.setAddress(document.getOnlyField("address").getText()); place.setLocation(new GeoPt((float) p.getLatitude(), (float) p.getLongitude())); // GeoPoints are not implemented on dev server and latitude and // longitude are set to zero // But since those are doubles let's play safe // and use double comparison with epsilon set to EPSILON if (Math.abs(p.getLatitude()) <= EPSILON && Math.abs(p.getLongitude()) <= EPSILON) { // set a fake distance of 5+ km place.setDistanceInKilometers(FAKE_DISTANCE_FOR_DEV + places .size()); } else { double distance = distanceInMeters / METERS_IN_KILOMETER; try { distance = getDistanceInKm( p.getLatitude(), p.getLongitude(), location.getLatitude(), location.getLongitude()); } catch (Exception e) { LOG.warning("Exception when calculating a distance: " + e .getMessage()); } place.setDistanceInKilometers(distance); } places.add(place); } return places; }
Example #5
Source File: FieldTest.java From appengine-tck with Apache License 2.0 | 4 votes |
@Test public void testDocFields() throws Exception { String indexName = "test-doc-fields"; Index index = searchService.getIndex(IndexSpec.newBuilder().setName(indexName)); delDocs(index); Builder docBuilder = Document.newBuilder(); Field field = Field.newBuilder().setName("textfield").setText("text field").build(); docBuilder.addField(field); field = Field.newBuilder().setName("numberfield").setNumber(123).build(); docBuilder.addField(field); Date now = new Date(); field = Field.newBuilder().setName("datefield").setDate(now).build(); docBuilder.addField(field); field = Field.newBuilder().setName("htmlfield").setHTML("<html>html field</html>").build(); docBuilder.addField(field); User currentUser = new User("[email protected]", "appenginetest.com"); field = Field.newBuilder().setName("atomfield").setAtom(currentUser.getAuthDomain()).build(); docBuilder.addField(field); GeoPoint geoPoint = new GeoPoint((double) -10, 10.000001); field = Field.newBuilder().setName("geofield").setGeoPoint(geoPoint).build(); docBuilder.addField(field); index.put(docBuilder); sync(); Results<ScoredDocument> result = searchDocs(index, "", 0); assertEquals(1, result.getNumberReturned()); ScoredDocument doc = result.iterator().next(); Field retField = doc.getOnlyField("textfield"); assertEquals(FieldType.TEXT, retField.getType()); assertEquals("textfield", retField.getName()); assertEquals("text field", retField.getText()); retField = doc.getOnlyField("numberfield"); assertEquals(FieldType.NUMBER, retField.getType()); assertEquals(new Double("123"), retField.getNumber()); retField = doc.getOnlyField("datefield"); assertEquals(FieldType.DATE, retField.getType()); assertEquals(now, retField.getDate()); retField = doc.getOnlyField("htmlfield"); assertEquals(FieldType.HTML, retField.getType()); assertEquals("<html>html field</html>", retField.getHTML()); retField = doc.getOnlyField("atomfield"); assertEquals(FieldType.ATOM, retField.getType()); assertEquals(currentUser.getAuthDomain(), retField.getAtom()); retField = doc.getOnlyField("geofield"); assertEquals(FieldType.GEO_POINT, retField.getType()); assertEquals(-10, retField.getGeoPoint().getLatitude(), 0); assertEquals(10.000001, retField.getGeoPoint().getLongitude(), 0.000000); }
Example #6
Source File: PlacesHelper.java From solutions-mobile-shopping-assistant-backend-java with Apache License 2.0 | 4 votes |
static List<PlaceInfo> getPlaces(GeoPt location, long distanceInMeters, int resultCount) { // TODO(user): Use memcache String geoPoint = "geopoint(" + location.getLatitude() + ", " + location.getLongitude() + ")"; String query = "distance(place_location, " + geoPoint + ") < " + distanceInMeters; String locExpr = "distance(place_location, " + geoPoint + ")"; SortExpression sortExpr = SortExpression.newBuilder() .setExpression(locExpr) .setDirection(SortExpression.SortDirection.ASCENDING) .setDefaultValueNumeric(distanceInMeters + 1) .build(); Query searchQuery = Query.newBuilder().setOptions(QueryOptions.newBuilder() .setSortOptions(SortOptions.newBuilder().addSortExpression(sortExpr))).build(query); Results<ScoredDocument> results = getIndex().search(searchQuery); if (results.getNumberFound() == 0) { // geo-location doesn't work under dev_server if (environment.value() == Development) { // return all documents results = getIndex().search("value > 0"); } } List<PlaceInfo> places = new ArrayList<PlaceInfo>(); for (ScoredDocument document : results) { if (places.size() >= resultCount) { break; } GeoPoint p = document.getOnlyField("place_location").getGeoPoint(); PlaceInfo place = new PlaceInfo(); place.setplaceID(document.getOnlyField("id").getText()); place.setName(document.getOnlyField("name").getText()); place.setAddress(document.getOnlyField("address").getText()); place.setLocation(new GeoPt((float) p.getLatitude(), (float) p.getLongitude())); // GeoPoints are not implemented on dev server and latitude and longitude are set to zero // But since those are doubles let's play safe // and use double comparison with epsilon set to 0.0001 if (Math.abs(p.getLatitude()) <= 0.0001 && Math.abs(p.getLongitude()) <= 0.0001) { // set a fake distance of 5+ km place.setDistanceInKilometers(5 + places.size()); } else { double distance = distanceInMeters / 1000; try { distance = getDistanceInKm( p.getLatitude(), p.getLongitude(), location.getLatitude(), location.getLongitude()); } catch (Exception e) { log.warning("Exception when calculating a distance: " + e.getMessage()); } place.setDistanceInKilometers(distance); } places.add(place); } return places; }