Class Geography
- Namespace
- YndigoBlue.Velocity.Model
- Assembly
- YndigoBlue.Velocity.dll
Represents geographic spatial data with coordinate reference system (SRID) support for earth-based locations.
public class Geography : IEquatable<Geometry>, IEquatable<Geography>
- Inheritance
-
Geography
- Implements
-
IEquatable<Geometry>
Examples
Creating and storing a geographic point (city location):
using NetTopologySuite.Geometries;
using (Manager manager = new Manager(connection))
{
manager.LoadSchema("my_schema");
Schema schema = manager.GetSchema("my_schema");
Table citiesTable = schema.CreateTable("cities");
citiesTable.AddColumn("id", DataType.Integer);
citiesTable.AddColumn("name", DataType.VarChar, size: 100);
citiesTable.AddColumn("location", DataType.Geography);
manager.BuildSchema(schema);
// Insert San Francisco's location (longitude, latitude in WGS 84)
Record<Table> record = new Record<Table>(citiesTable);
record.SetFieldInteger("id", 1);
record.SetFieldVarChar("name", "San Francisco");
Point sfLocation = new Point(-122.4194, 37.7749);
sfLocation.SRID = 4326; // WGS 84 coordinate system
Geography sfGeography = new Geography(sfLocation);
record.SetFieldGeography("location", sfGeography);
manager.AddRecord(record);
}
Retrieving and using geographic data:
Query query = new Query();
query.AddFromItem(citiesTable);
query.AddSelectItem(citiesTable["name"]);
query.AddSelectItem(citiesTable["location"]);
IEnumerable<Result> results = manager.Retrieve(query);
foreach (Result result in results)
{
string cityName = result.GetFieldVarChar("name");
Geography location = result.GetFieldGeography("location");
Console.WriteLine($"{cityName}:");
Console.WriteLine($" Longitude: {location.Coordinate.X}");
Console.WriteLine($" Latitude: {location.Coordinate.Y}");
Console.WriteLine($" SRID: {location.SRID}");
Console.WriteLine($" WKT: {location.AsText()}");
}
Working with polygons (geographic boundaries):
// Define a polygon representing a park boundary
Coordinate[] parkBoundary = new Coordinate[]
{
new Coordinate(-122.42, 37.78),
new Coordinate(-122.42, 37.77),
new Coordinate(-122.41, 37.77),
new Coordinate(-122.41, 37.78),
new Coordinate(-122.42, 37.78) // Close the polygon
};
LinearRing ring = new LinearRing(parkBoundary);
Polygon parkPolygon = new Polygon(ring);
parkPolygon.SRID = 4326;
Geography parkGeography = new Geography(parkPolygon);
Record<Table> record = new Record<Table>(parksTable);
record.SetFieldVarChar("name", "Golden Gate Park");
record.SetFieldGeography("boundary", parkGeography);
manager.AddRecord(record);
Working with lines (routes, roads):
// Define a route as a line
Coordinate[] routePoints = new Coordinate[]
{
new Coordinate(-122.42, 37.78),
new Coordinate(-122.41, 37.77),
new Coordinate(-122.40, 37.76)
};
LineString routeLine = new LineString(routePoints);
routeLine.SRID = 4326;
Geography routeGeography = new Geography(routeLine);
Console.WriteLine($"Route length: {routeGeography.Length} degrees");
Console.WriteLine($"Start point: {routeGeography.Coordinates[0]}");
Console.WriteLine($"End point: {routeGeography.Coordinates[routeGeography.NumPoints - 1]}");
Using geography properties:
Geography cityLocation = result.GetFieldGeography("location");
// Access spatial properties
bool isEmpty = cityLocation.IsEmpty;
bool isValid = cityLocation.IsValid;
double area = cityLocation.Area; // For polygons
double length = cityLocation.Length; // For lines
Point centroid = cityLocation.Centroid; // Center point
// Get coordinates
Coordinate singlePoint = cityLocation.Coordinate; // First coordinate
Coordinate[] allPoints = cityLocation.Coordinates; // All coordinates
int pointCount = cityLocation.NumPoints;
// Get boundary (for polygons)
Geometry boundary = cityLocation.Boundary;
// Get envelope (bounding box)
Geometry envelope = cityLocation.Envelope;
Converting to/from text formats:
Geography location = result.GetFieldGeography("location");
// Get Well-Known Text (WKT) representation
string wkt = location.AsText();
// Output: "POINT (-122.4194 37.7749)"
// Get Well-Known Binary (WKB) representation
byte[] wkb = location.AsBinary();
// These can be stored or transmitted and later reconstructed
Console.WriteLine($"Location as WKT: {wkt}");
Remarks
WARNING: Geospatial data types (Geography and Geometry) are not available in the Community Edition of Velocity. They require the Full Edition.
Geography wraps NetTopologySuite's Geometry to represent spatial data that uses geographic coordinate systems (latitude/longitude) rather than planar coordinates. Geography types are used for real-world locations on the Earth's surface.
Geography vs Geometry:
- Geography: Uses latitude/longitude coordinates (SRID typically 4326 for WGS 84). Calculations account for Earth's curvature. Used for GPS coordinates, maps, locations.
- Geometry: Uses planar (X,Y) coordinates (SRID 0 or custom). Calculations use Euclidean math. Used for CAD drawings, game maps, abstract shapes.
Geography supports all standard spatial types from NetTopologySuite:
- Point: Single location (e.g., a building, city)
- LineString: Connected line segments (e.g., a road, river)
- Polygon: Closed area (e.g., country boundary, park)
- MultiPoint, MultiLineString, MultiPolygon: Collections of the above
- GeometryCollection: Mixed collection of spatial objects
Coordinate Reference Systems (SRID):
The SRID (Spatial Reference Identifier) defines the coordinate system:
- 4326: WGS 84 - Standard GPS coordinates (longitude, latitude)
- 3857: Web Mercator - Used by web mapping (Google Maps, OpenStreetMap)
- Custom: Other EPSG codes for regional coordinate systems
Geography is stored in the database using database-specific spatial types and can be queried using spatial functions and indexes via SpatialIndex.
Constructors
Properties
Methods
- Equals(Geometry)
Indicates whether the current object is equal to another object of the same type.
- Equals(Geography)
Indicates whether the current object is equal to another object of the same type.
- ToString()
Returns a string that represents the current object.