Vector formats
The geobase
package supports common geospatial data formats:
- GeoJSON text representation for feature and geometry objects
- WKT text representation for geometry objects
- WKB binary representation for geometry objects
The latest version 1.1.0 (published on April 2024) provides also following extensions or variants:
- Newline-delimited GeoJSON providing an optimized format for large datasets with large number of features
- Extended WKT (EWKT) supporting geometries with a coordinate reference system id (SRID)
- Extended WKB (EWKB) supporting geometries with a coordinate reference system id (SRID)
Format classes
Some of the formats support encodings both for simple geometries and geospatial features, and some only for one of these object types.
The table below shows support in each data format and which static constant
instance of TextFormat
or BinaryFormat
should be used to reference a certain
format.
Vector format | Simple geometries | Geospatial features |
---|---|---|
GeoJSON | GeoJSON.geometry | GeoJSON.feature |
Newline-delimited GeoJSON | - | GeoJSONL.feature |
WKT (and EWKT) | WKT.geometry | - |
WKB (and EWKB) | WKB.geometry | - |
GeoJSON
, GeoJSONL
and WKT
are text formats extending the base class
TextFormat
.
More specifically for example GeoJSON provides the type
TextFormat<GeometryContent>
for simple geometries and
TextFormat<FeatureContent>
for geospatial features.
For simple geometries WKT.geometry
is a text format of the type
TextFormat<GeometryContent>
and WKB.geometry
is a binary format of the type
BinaryFormat<GeometryContent>
.
Thatās about class theory, next letās see how these classes are used in action.
GeoJSON
WGS 84 longitude/latitude
As already described GeoJSON is a format for encoding geometry, feature and feature collection objects. The data structures introduced on previous simple geometries and geospatial features chapters are modelled to support encoding and decoding GeoJSON data.
The format is specified by the RFC 7946 standard. Geometry objects use WGS 84 longitude/latitude geographic coordinates. Also alternative coordinate reference systems can be used when involved parties have a prior arrangement of using other systems.
In this package the default coordinate reference system (WGS 84 with longitude
before latitude) can also be referenced by the CoordRefSys.CRS84
constant.
Normally when parsing and writing content in this default coordinate system you
donāt need to specify a crs however.
Parsing a feature collection from a GeoJSON text representation is demonstrated below.
You can also parse separate geometry objects without any parent feature objects.
Encoding GeoJSON text representation from feature and geometry objects is an
easy task to do also. Just call the toText()
method on any such object
instance.
Newline-delimited GeoJSON
GeoJSON is a perfect textual data format for sharing geospatial structured data between servers and clients in web or mobile apps. However it may become suboptimal in use cases that use very large number of geospatial feature objects or that stream such objects one by one.
One solution for these scenarios is called Newline-delimited GeoJSON.
Regardless of what itās called or what minor variations there are in specifications mentioned the key idea is simple.
A text file conforming to this format represents one feature collection (however without FeatureCollection element encoded). Such a file may contain any number of features that are separated by the newline character ā\nā. Each line separately must contain a valid GeoJSON feature object.
A newline-delimited GeoJSON representation of same sample data as used already in samples earlier is presented below.
Decoding and encoding newline-delimited GeoJSON is not any harder than using the standard one. Just remember to use a format called āGeoJSONLā instead of āGeoJSONā.
Advantages of using GeoJSONL over the standard GeoJSON include efficiency when streaming or storing very large number geospatial features. Itās also much simpler to decode newline-delimited GeoJSON data than hierarchically structured standard GeoJSON data. A client could also skip some features on a stream without parsing all data.
The decoder provided by geobase
supports reading features delimited by
newline (\n
), carriage-return followed by newline (\r\n
) and
record-separator (RS) characters. By default the encoder delimits features using
the \n
character.
Alternative coordinate reference systems
When using GeoJSON to represent geospatial data in āalternative coordinate reference systemsā, such a system must be explicitely defined (and known) before reading in or before writing out GeoJSON content.
As described in the coordinates summary internally all classes in this package handle coordinate axis order so that x (or longitude) is always before y (or latitude). However some coordinate reference systems require other axis order when representing geometries in external data formats.
The CoordRefSys
class introduced in the section about
coordinate reference systems
has the swapXY
method that tells how axis order should be handled for a
certain coordinate reference system when dealing with external data
representations (like the current specification of GeoJSON) that do not specify
a generic axis order for alternative coordinate reference systems.
The sample below demonstrates the logic:
WKT
Well-known text representation of geometry
Well-known text representation of geometry (WKT) is a text markup language for representing vector geometry objects (but not applicable for feature objects). Itās specified by the Simple Feature Access - Part 1: Common Architecture published by OGC.
WKT is used, for example, by PostGIS that provides functions converting geometries to and from a WKT representation. This allows geometry objects to be easily human readable. WKT geometries are used also by many other OGC specifications and supported by different geospatial applications. In web you can find even more guides how to use WKT.
The syntax for WKT is quite simple. A geometry element is identified by a tag (ie. POINT for point geometries, LINESTRING for polylines and POLYGON for polygons) and an optional specifier denoting extra coordinate dimension (Z or M). Coordinate values are included inside parentheses. For 2D coordinates X is always the first and Y the second coordinate value (and when representing geographic coordinates then X = longitude and Y = latitude in this order).
The snippet below shows Dart code parsing 2D, 3D, measured and 4D point geometries represented as WKT text.
If geometry type is not known when parsing text from external datasources, you
can use GeometryBuilder
to parse geometries of any type:
Just like with GeoJSON, also writing an encoded text representation is supported too.
Itās possible to encode geometry data as WKT text also without creating geometry objects first. However this requires accessing an encoder instance from the WKT format, and then writing content to that encoder. See sample below:
Such format encoders (and formatting without geometry objects) are suppported also for GeoJSON. However for both WKT and GeoJSON encoding might be easier using concrete geometry model objects.
Extended WKT (EWKT)
For some use cases the standard WKT is not enough.
Extended WKT (or EWKT) is a PostGIS-specific flavor of this format. It has an alternative syntax for specifying extra coordinates (Z and M), and it allows encoding also a coordinate reference system (SRID).
Let the sample below tell more!
WKB
Well-known binary
According to the Wikipedia description a binary equivalent for WKT, known as well-known binary (WKB), is used to transfer and store the same information in a more compact form convenient for computer processing but that is not human-readable.
The standard WKB binary representation supports encoding the same geometry and coordinate types as the WKT text representation.
Extended WKB (EWKB)
The WKB
class supports also a variant of WKB called Extended WKB (EWKB) that
is a PostGIS-specific format. Geometry types for 3D (with z coordinates) and
measured (with m) coordinates are encoded differently to the standard WKB. Itās
also possible to encode an optional SRID (or coordinate reference system id) to
EWKB data thatās not possible with the standard WKB.
More information about EWKB can be read from PostGIS or GEOS software documentation.
Basic sample on WKB
The sample below shows how to deal with data that could contain either WKB or EWKB binary data.
Advanced sample on WKB
Two different approaches to use WKB encoders and decoders are presented in this section.
First a not-so-simple sample below processes data for demo purposes in following steps:
- write geometry content as a source
- encode content as WKB bytes
- decode those WKB bytes
- WKT encoder receives input from WKB decoder, and prints WKT text
The solution above can be simplified a lot by using geometry model objects:
This second solution uses same formats, encoders, decoders and builders as the first one, but the details of using them is hidden under an easier interface.