Geographic information including coordinates, places, and location metadata associated with tweets.
GeoJSON-formatted coordinate information representing the precise geographic location where a tweet was posted.
/**
* Represents geographic coordinates of a tweet in GeoJSON format.
* Contains longitude and latitude coordinates as reported by the user or client.
*/
public class Coordinates {
/**
* Default constructor that initializes coordinate type as "point".
*/
public Coordinates();
/**
* Get the coordinate array [longitude, latitude].
* @return Array containing [longitude, latitude] as doubles
*/
public double[] getCoordinates();
/**
* Set the coordinate array [longitude, latitude].
* @param coordinates Array containing [longitude, latitude] as doubles
*/
public void setCoordinates(double[] coordinates);
/**
* Set coordinates using individual longitude and latitude values.
* @param longitude Longitude value (first in GeoJSON format)
* @param latitude Latitude value (second in GeoJSON format)
*/
public void setCoordinates(double longitude, double latitude);
/**
* Get the coordinate type (always "point" for Twitter coordinates).
* @return Coordinate type string
*/
public String getType();
/**
* String representation of coordinates for debugging.
* @return Human-readable coordinate string
*/
public String toString();
}Named location information representing specific places associated with tweets, providing context beyond precise coordinates.
/**
* Represents named locations with geographic context.
* Provides place information that can be attached to tweets by specifying a place_id.
*/
public class Places {
/**
* Default constructor that initializes attributes and bounding box.
*/
public Places();
/**
* Get the unique identifier for this place.
* @return Place ID string
*/
public String getId();
/**
* Set the unique identifier for this place.
* @param id Place ID string
*/
public void setId(String id);
/**
* Get the short name of this place.
* @return Place name
*/
public String getName();
/**
* Set the short name of this place.
* @param name Place name
*/
public void setName(String name);
/**
* Get the full descriptive name of this place.
* @return Full place name
*/
public String getFull_name();
/**
* Set the full descriptive name of this place.
* @param full_name Full place name
*/
public void setFull_name(String full_name);
/**
* Get the country name for this place.
* @return Country name
*/
public String getCountry();
/**
* Set the country name for this place.
* @param country Country name
*/
public void setCountry(String country);
/**
* Get the ISO 3166-1 alpha-2 country code.
* @return Two-letter country code
*/
public String getCountry_code();
/**
* Set the ISO 3166-1 alpha-2 country code.
* @param country_code Two-letter country code
*/
public void setCountry_code(String country_code);
/**
* Get the type of place (city, neighborhood, poi, etc.).
* @return Place type string
*/
public String getPlace_type();
/**
* Set the type of place (city, neighborhood, poi, etc.).
* @param place_type Place type string
*/
public void setPlace_type(String place_type);
/**
* Get the URL for more information about this place.
* @return Place URL
*/
public String getUrl();
/**
* Set the URL for more information about this place.
* @param url Place URL
*/
public void setUrl(String url);
/**
* Get additional attributes for this place.
* @return Attributes object containing detailed place information
*/
public Attributes getAttributes();
/**
* Set additional attributes for this place.
* @param attributes Attributes object containing detailed place information
*/
public void setAttributes(Attributes attributes);
/**
* Get the bounding box that encompasses this place.
* @return BoundingBox object defining geographic boundaries
*/
public BoundingBox getBounding_box();
/**
* Set the bounding box that encompasses this place.
* @param bounding_box BoundingBox object defining geographic boundaries
*/
public void setBounding_box(BoundingBox bounding_box);
}Additional detailed attributes providing extended information about places including address components and contact information.
/**
* Additional attributes for places providing detailed location information.
* Contains address components, contact information, and other place metadata.
*/
public class Attributes {
/**
* Default constructor.
*/
public Attributes();
/**
* Get the street address for this place.
* @return Street address
*/
public String getStreet_address();
/**
* Set the street address for this place.
* @param street_address Street address
*/
public void setStreet_address(String street_address);
/**
* Get the locality (city/town) for this place.
* @return Locality name
*/
public String getLocality();
/**
* Set the locality (city/town) for this place.
* @param locality Locality name
*/
public void setLocality(String locality);
/**
* Get the region (state/province) for this place.
* @return Region name
*/
public String getRegion();
/**
* Set the region (state/province) for this place.
* @param region Region name
*/
public void setRegion(String region);
/**
* Get the ISO 3166-1 alpha-3 country code.
* @return Three-letter country code
*/
public String getIso3();
/**
* Set the ISO 3166-1 alpha-3 country code.
* @param iso3 Three-letter country code
*/
public void setIso3(String iso3);
/**
* Get the postal code for this place.
* @return Postal code
*/
public String getPostal_code();
/**
* Set the postal code for this place.
* @param postal_code Postal code
*/
public void setPostal_code(String postal_code);
/**
* Get the phone number for this place.
* @return Phone number
*/
public String getPhone();
/**
* Set the phone number for this place.
* @param phone Phone number
*/
public void setPhone(String phone);
/**
* Get the URL for this place.
* @return Place URL
*/
public String getUrl();
/**
* Set the URL for this place.
* @param url Place URL
*/
public void setUrl(String url);
/**
* Get the application ID associated with this place.
* @return Application ID
*/
public String getAppId();
/**
* Set the application ID associated with this place.
* @param appId Application ID
*/
public void setAppId(String appId);
}Geographic boundaries defining the area encompassed by a place, represented as coordinate bounds.
/**
* Represents geographic boundaries for places as coordinate bounds.
* Defines the rectangular area that encompasses a place.
*/
public class BoundingBox {
/**
* Default constructor.
*/
public BoundingBox();
/**
* Constructor with list of coordinate points.
* @param points List of coordinate points [longitude, latitude]
*/
public BoundingBox(List<double[]> points);
/**
* Get the bounding box type (usually "Polygon").
* @return Bounding box type
*/
public String getType();
/**
* Set the bounding box type (usually "Polygon").
* @param type Bounding box type
*/
public void setType(String type);
/**
* Get the coordinate list defining the bounding box.
* @return List of coordinate point lists
*/
public List<List<double[]>> getCoordinates();
/**
* Set the coordinate list defining the bounding box.
* @param coordinates List of coordinate point lists
*/
public void setCoordinates(List<List<double[]>> coordinates);
}Usage Examples:
import org.apache.flink.contrib.tweetinputformat.model.tweet.Coordinates;
import org.apache.flink.contrib.tweetinputformat.model.places.*;
import java.util.List;
// Process geographic information
Tweet tweet = // ... get tweet
// Check for precise coordinates
Coordinates coords = tweet.getCoordinates();
if (coords != null) {
double[] coordinates = coords.getCoordinates();
double longitude = coordinates[0];
double latitude = coordinates[1];
System.out.printf("Tweet location: %.6f, %.6f%n", longitude, latitude);
System.out.println("Coordinate type: " + coords.getType());
}
// Check for place information
Places place = tweet.getPlace();
if (place != null) {
System.out.println("Place: " + place.getName());
System.out.println("Full name: " + place.getFull_name());
System.out.println("Country: " + place.getCountry() + " (" + place.getCountry_code() + ")");
System.out.println("Place type: " + place.getPlace_type());
// Access detailed attributes
Attributes attributes = place.getAttributes();
if (!attributes.getStreet_address().isEmpty()) {
System.out.println("Address: " + attributes.getStreet_address());
System.out.println("City: " + attributes.getLocality());
System.out.println("Region: " + attributes.getRegion());
System.out.println("Postal code: " + attributes.getPostal_code());
}
if (!attributes.getPhone().isEmpty()) {
System.out.println("Phone: " + attributes.getPhone());
}
if (!attributes.getUrl().isEmpty()) {
System.out.println("URL: " + attributes.getUrl());
}
// Access bounding box
BoundingBox bbox = place.getBounding_box();
if (bbox != null) {
System.out.println("Bounding box type: " + bbox.getType());
// Process bounding box coordinates as needed...
}
}
// Determine location availability
boolean hasLocation = (coords != null) || (place != null);
if (hasLocation) {
System.out.println("Tweet has geographic information");
} else {
System.out.println("No geographic information available");
}Common patterns for analyzing geographic data in stream processing:
// Filter tweets with precise coordinates
tweets.filter(tweet -> tweet.getCoordinates() != null)
.map(tweet -> {
double[] coords = tweet.getCoordinates().getCoordinates();
return new GeoTweet(
tweet.getId_str(),
tweet.getText(),
coords[0], // longitude
coords[1] // latitude
);
});
// Filter tweets by country
tweets.filter(tweet -> {
Places place = tweet.getPlace();
return place != null && "US".equals(place.getCountry_code());
});
// Extract place types
tweets.filter(tweet -> tweet.getPlace() != null)
.map(tweet -> tweet.getPlace().getPlace_type())
.countByValue();
// Geographic clustering by place names
tweets.filter(tweet -> tweet.getPlace() != null)
.map(tweet -> tweet.getPlace().getFull_name())
.countByValue();
// Region-based analysis
tweets.filter(tweet -> {
Places place = tweet.getPlace();
return place != null && place.getAttributes() != null;
}).map(tweet -> {
Attributes attrs = tweet.getPlace().getAttributes();
return new RegionTweet(
tweet.getId_str(),
attrs.getLocality(),
attrs.getRegion(),
tweet.getPlace().getCountry_code()
);
});
// Distance calculation from reference point
double refLon = -74.0060; // New York City
double refLat = 40.7128;
tweets.filter(tweet -> tweet.getCoordinates() != null)
.map(tweet -> {
double[] coords = tweet.getCoordinates().getCoordinates();
double distance = calculateDistance(refLat, refLon, coords[1], coords[0]);
return new DistanceTweet(tweet.getId_str(), distance);
});Important considerations for working with Twitter coordinates:
// Safe coordinate extraction
public static class GeoUtils {
public static double[] extractCoordinates(Tweet tweet) {
Coordinates coords = tweet.getCoordinates();
return coords != null ? coords.getCoordinates() : null;
}
public static String extractCountryCode(Tweet tweet) {
Places place = tweet.getPlace();
return place != null ? place.getCountry_code() : null;
}
public static double calculateDistance(double lat1, double lon1, double lat2, double lon2) {
// Haversine formula implementation
final double R = 6371; // Earth's radius in kilometers
double dLat = Math.toRadians(lat2 - lat1);
double dLon = Math.toRadians(lon2 - lon1);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
return R * c;
}
}