public static Location fromPlace(twitter4j.Place place) {

    Location location = new Location();

    if (place.getCountry() != null) {
      location.setCountry(place.getCountry());
    }

    if (place.getCountryCode() != null) {
      location.setCountryCode(place.getCountryCode());
    }

    location.setFullname(place.getFullName());

    location.setLocationType(place.getPlaceType());

    location.setUrl(place.getURL());

    if (place.getGeometryCoordinates() != null) {
      if (place.getGeometryCoordinates().length > 0) {
        if (place.getGeometryCoordinates()[0].length > 0) {
          GeoLocation loc = place.getGeometryCoordinates()[0][0];

          if (loc != null) {
            location.setLatitude(loc.getLatitude());
            location.setLongitude(loc.getLongitude());
          }
        }
      }
    }

    return location;
  }
Esempio n. 2
0
 private static Record buildPlace(Schema schemaPlace, Place place) {
   GenericRecordBuilder builderPlace = new GenericRecordBuilder(schemaPlace);
   builderPlace.set("country", place.getCountry());
   builderPlace.set("country_code", place.getCountryCode());
   builderPlace.set("full_name", place.getFullName());
   builderPlace.set("id", place.getId());
   builderPlace.set("name", place.getName());
   builderPlace.set("place_type", place.getPlaceType());
   builderPlace.set("bounding_box_type", place.getBoundingBoxType());
   builderPlace.set("geometry_type", place.getGeometryType());
   builderPlace.set("street_address", place.getStreetAddress());
   builderPlace.set("url", place.getURL());
   builderPlace.set("bounding_box", getPlaceCoordinates(place.getBoundingBoxCoordinates()));
   if (place.getGeometryCoordinates() != null)
     builderPlace.set("geometry", getPlaceCoordinates(place.getGeometryCoordinates()));
   /*
    * if (place.getContainedWithIn() != null) { List<GenericRecord>
    * listPlaceContainedWithin = new ArrayList<GenericRecord>(); for (Place
    * place2 : place.getContainedWithIn()) {
    * listPlaceContainedWithin.add(buildPlace(schemaPlace, place2)); }
    * builderPlace.set("contained_within", listPlaceContainedWithin); }
    */
   return builderPlace.build();
 }
  public void testPlaceAsJSON() throws Exception {
    List<Place> places =
        PlaceJSONImpl.createPlaceList(
            getJSONObjectFromClassPath("/dao/reverse-geocode.json")
                .getJSONObject("result")
                .getJSONArray("places"),
            null,
            conf);
    Place place = places.get(0);
    Assert.assertEquals("SoMa", place.getName());
    Assert.assertEquals("US", place.getCountryCode());
    Assert.assertEquals("2b6ff8c22edd9576", place.getId());
    Assert.assertEquals("", place.getCountry());
    Assert.assertEquals("neighborhood", place.getPlaceType());
    Assert.assertEquals("http://api.twitter.com/1/geo/id/2b6ff8c22edd9576.json", place.getURL());
    Assert.assertEquals("SoMa, San Francisco", place.getFullName());
    Assert.assertEquals("Polygon", place.getBoundingBoxType());
    GeoLocation[][] boundingBox = place.getBoundingBoxCoordinates();
    Assert.assertEquals(1, boundingBox.length);
    Assert.assertEquals(4, boundingBox[0].length);
    Assert.assertEquals(37.76893497, boundingBox[0][0].getLatitude());
    Assert.assertEquals(-122.42284884, boundingBox[0][0].getLongitude());
    Assert.assertEquals(37.76893497, boundingBox[0][1].getLatitude());
    Assert.assertEquals(-122.3964, boundingBox[0][1].getLongitude());
    Assert.assertEquals(37.78752897, boundingBox[0][2].getLatitude());
    Assert.assertEquals(-122.3964, boundingBox[0][2].getLongitude());
    Assert.assertEquals(37.78752897, boundingBox[0][3].getLatitude());
    Assert.assertEquals(-122.42284884, boundingBox[0][3].getLongitude());
    Assert.assertNull(place.getGeometryType());
    Assert.assertNull(place.getGeometryCoordinates());

    Place[] containedWithinArray = place.getContainedWithIn();
    Assert.assertEquals(1, containedWithinArray.length);
    Place containedWithin = containedWithinArray[0];
    Assert.assertNull(containedWithin.getContainedWithIn());
    Assert.assertEquals("San Francisco", containedWithin.getName());
    Assert.assertEquals("US", containedWithin.getCountryCode());
    Assert.assertEquals("5a110d312052166f", containedWithin.getId());
    Assert.assertEquals("", containedWithin.getCountry());
    Assert.assertEquals("city", containedWithin.getPlaceType());
    Assert.assertEquals(
        "http://api.twitter.com/1/geo/id/5a110d312052166f.json", containedWithin.getURL());
    Assert.assertEquals("San Francisco", containedWithin.getFullName());
    boundingBox = containedWithin.getBoundingBoxCoordinates();
    Assert.assertEquals("Polygon", place.getBoundingBoxType());
    Assert.assertEquals(1, boundingBox.length);
    Assert.assertEquals(4, boundingBox[0].length);
    Assert.assertEquals(37.70813196, boundingBox[0][0].getLatitude());
    Assert.assertEquals(-122.51368188, boundingBox[0][0].getLongitude());
    Assert.assertEquals(37.70813196, boundingBox[0][1].getLatitude());
    Assert.assertEquals(-122.35845384, boundingBox[0][1].getLongitude());
    Assert.assertEquals(37.83245301, boundingBox[0][2].getLatitude());
    Assert.assertEquals(-122.35845384, boundingBox[0][2].getLongitude());
    Assert.assertEquals(37.83245301, boundingBox[0][3].getLatitude());
    Assert.assertEquals(-122.51368188, boundingBox[0][3].getLongitude());

    Assert.assertNull(place.getGeometryType());
    Assert.assertNull(place.getGeometryCoordinates());

    place = new PlaceJSONImpl(getJSONObjectFromClassPath("/dao/5a110d312052166f.json"), null);
    Assert.assertNotNull(place.getGeometryType());
    Assert.assertNotNull(place.getGeometryCoordinates());

    // Test that a geo object with geometry type "Point" works.
    place = new PlaceJSONImpl(getJSONObjectFromClassPath("/dao/3c6797665e2d42eb.json"), null);
    Assert.assertEquals(place.getGeometryType(), "Point");
    Assert.assertNotNull(place.getGeometryCoordinates());

    place = new PlaceJSONImpl(getJSONObjectFromClassPath("/dao/c3f37afa9efcf94b.json"), null);
    // MultiPolygon is not supported by twitter4j yet, so we set geometryType to null
    Assert.assertNull(place.getGeometryType());
    Assert.assertNull(place.getGeometryCoordinates());
  }