GeoDesk for C++
Fast and storage-efficient spatial database engine for OpenStreetMap features
Loading...
Searching...
No Matches
geodesk::Features Class Reference

A collection of geographic features. More...

#include <Features.h>

Public Member Functions

 Features (const char *golFile)
 Creates a collection that contains all features in the given Geographic Object Library.
 
 Features (const Features &other)
 Creates a collection with all the features in the other collection.
 
State and Membership
 operator bool () const
 Returns true if this collection contains at least one Feature.
 
bool operator! () const
 Returns true if this collection is empty.
 
bool contains (const Feature &feature) const
 Returns true if this collection contains the given Feature.
 
Filtering by type and tags
Features operator() (const char *query) const
 Only features that match the given query.
 
Nodes nodes () const
 Only nodes.
 
Nodes nodes (const char *query)
 Only nodes that match the given query.
 
Ways ways () const
 Only ways.
 
Ways ways (const char *query) const
 Only ways that match the given query.
 
Relations relations () const
 Only relations.
 
Relations relations (const char *query) const
 Only relations that match the given query.
 
Retrieving Features
std::optional< Feature > first () const
 Returns the first Feature in this collection, or std::nullopt if the collection is empty.
 
Feature one () const
 Returns the one and only Feature in this collection.
 
void addTo (std::vector< Feature > &features) const
 Appends the Feature objects in this collection to the given std::vector.
 
void addTo (std::vector< FeaturePtr > &features) const
 Appends the Feature objects in this collection to the given std::vector as FeaturePtr pointers.
 
Scalar Queries
uint64_t count () const
 Returns the total number of features in this collection.
 
double length () const
 Calculates the total length (in meters) of the features in this collection.
 
double area () const
 Calculates the total area (in square meters) of the features in this collection.
 
Spatial Filters
Features operator() (const Box &box) const
 Only features whose bounding box intersects the given bounding box.
 
Features operator() (Coordinate xy) const
 Only features whose bounding box contains the given Coordinate.
 
Features operator() (const Feature &feature) const
 Only features whose geometry intersects with the given Feature (short form of intersecting())
 
Features intersecting (const Feature &feature) const
 Only features whose geometry intersects with the given Feature.
 
Features within (const Feature &feature) const
 Only features that lie entirely inside the geometry of the given Feature.
 
Features containing (const Feature &feature) const
 Only features whose geometry contains the given Feature.
 
Features containing (Coordinate xy) const
 Only features whose geometry contains the given Coordinate.
 
Features containingLonLat (double lon, double lat) const
 Only features whose geometry contains the given location.
 
Features crossing (const Feature &feature) const
 Only features whose geometry crosses the given Feature.
 
Features maxMetersFrom (double distance, Coordinate xy) const
 Only features whose closest point lies within distance meters of xy.
 
Features maxMetersFrom (double distance, double lon, double lat) const
 Only features whose closest point lies within distance meters of the given location.
 
Topological Filters
Features nodesOf (const Feature &feature) const
 Only nodes that belong to the given Way.
 
Features membersOf (const Feature &feature) const
 Only features that belong to the given Relation.
 
Features parentsOf (const Feature &feature) const
 Only features that are parent relations of the given Feature (or parent ways of the given Node).
 
Features connectedTo (const Feature &feature) const
 Only features that share a common node with the given Feature.
 
Filtering with Predicate
template<typename Predicate >
Features filter (Predicate predicate) const
 Only features that match the given predicate.
 
Access to the Low-Level API
FeatureStore * store () const noexcept
 Returns a pointer to the FeatureStore which contains the features in this collection.
 

Detailed Description

A collection of geographic features.

A Features object isn't a classical container; it doesn't actually hold any Feature objects, but merely describes which features should be fetched from a Geographic Object Library. A query is only executed whenever the Features object is iterated, assigned to a container, or when one of its scalar functions (such as count() or length()) is called. This also means that query results are not cached: If you call count() prior to iterating over the Features, two queries will be executed.

Features objects are lightweight and suitable for passing by value. They are threadsafe and can be freely shared among different threads. Their various matchers and filters (and associated resources such as temporary indexes) are shared via refcounting.

To start working with a GOL, simply create a Features object with the path of its file (the .gol extension may be omitted):

Features world("path/to/planet.gol");
A collection of geographic features.
Definition Features.h:111

This creates a FeatureStore that manages access to planet.gol. The world object now represents a collection of all features stored in the GOL (It is legal to create multiple Features objects that refer to the same GOL – they will share the same FeatureStore).

To obtain subsets, apply filters using () or by calling a named method – or intersect multiple Features object with &:

Features hotels = world("na[tourism=hotel]"); // All hotels
Features inParis = world.within(paris); // All features in Paris
// Only hotels in Paris:
Features hotelsInParis = hotels.within(paris);
// or...
Features hotelsInParis = hotels & inParis;
Features within(const Feature &feature) const
Only features that lie entirely inside the geometry of the given Feature.

Applying a filter creates a copy of the Features object, with the additional constraint. The original Features object is unaffected.

To retrieve the actual features:

// By iterating:
for(Feature hotel : hotelsInParis)
{
std::cout << hotel["name"] << std::endl;
}
// As a vector:
std::vector<Feature> hotelList = hotelsInParis;
// The one and only:
Feature paris = world("a[boundary=administrative]"
"[admin_level=8][name=Paris]").one();
// The first (if any):
std::optional<Feature> anyHotel = hotels.first();
std::optional< Feature > first() const
Returns the first Feature in this collection, or std::nullopt if the collection is empty.

Features has three subclasses: Nodes, Ways and Relations, which contain only Node, Way and Relation objects. Assigning a Features object to a subclass object implicitly filters the collection by type (Assigning a subtype to an incompatible type results in an empty collection):

Features world("world");
Relations busRoutes = // Only relations that
world("[type=route][route=bus]") // are tagged as bus routes
Nodes nodes = busRoutes; // empty collection
Nodes nodes() const
Only nodes.
Definition Nodes.h:11
Definition Relations.h:11

Upon destruction of last of the Features objects that refer to the same FeatureStore, the Geographic Object Library is automatically closed and all of the FeatureStore's resources are released (This differs from GeoDesk for Java, which requires GOLs to be closed explicitly).

Important: Once a GOL has been closed, all Feature objects retrieved via queries to that GOL are no longer valid; calling any of their methods will result in undefined behavior (The same applies to Tags, Tag, TagValue and StringValue objects).

Constructor & Destructor Documentation

◆ Features() [1/2]

geodesk::Features::Features ( const char * golFile)

Creates a collection that contains all features in the given Geographic Object Library.

Parameters
golFilepath of the GOL (.gol extension may be omitted)

◆ Features() [2/2]

geodesk::Features::Features ( const Features & other)

Creates a collection with all the features in the other collection.

Member Function Documentation

◆ addTo() [1/2]

void geodesk::Features::addTo ( std::vector< Feature > & features) const

Appends the Feature objects in this collection to the given std::vector.

◆ addTo() [2/2]

void geodesk::Features::addTo ( std::vector< FeaturePtr > & features) const

Appends the Feature objects in this collection to the given std::vector as FeaturePtr pointers.

◆ area()

double geodesk::Features::area ( ) const

Calculates the total area (in square meters) of the features in this collection.

Exceptions
QueryExceptionif one or more tiles that contain the geometry of a Relation are missing

◆ connectedTo()

Features geodesk::Features::connectedTo ( const Feature & feature) const

Only features that share a common node with the given Feature.

◆ containing() [1/2]

Features geodesk::Features::containing ( const Feature & feature) const

Only features whose geometry contains the given Feature.

Exceptions
QueryExceptionif one or more tiles that contain the geometry of a Relation are missing

◆ containing() [2/2]

Features geodesk::Features::containing ( Coordinate xy) const

Only features whose geometry contains the given Coordinate.

◆ containingLonLat()

Features geodesk::Features::containingLonLat ( double lon,
double lat ) const

Only features whose geometry contains the given location.

Parameters
londegrees longitude
latdegrees latitude

◆ contains()

bool geodesk::Features::contains ( const Feature & feature) const

Returns true if this collection contains the given Feature.

◆ count()

uint64_t geodesk::Features::count ( ) const

Returns the total number of features in this collection.

◆ crossing()

Features geodesk::Features::crossing ( const Feature & feature) const

Only features whose geometry crosses the given Feature.

Exceptions
QueryExceptionif one or more tiles that contain the geometry of a Relation are missing

◆ filter()

template<typename Predicate >
Features geodesk::Features::filter ( Predicate predicate) const

Only features that match the given predicate.

Parameters
predicateA callable object (e.g., lambda, function pointer, or functor) that defines the filtering logic. The callable must accept a Feature and return a bool.

Important: The provided predicate must be thread-safe, as it may be invoked concurrently.

// Find all parks whose area is at least 1 kmĀ²
// (one million square meters)
Features parks = world("a[leisure=park]");
Features largeParks = parks.filter([](Feature park)
{ return park.area() > 1'000'000; });
Features filter(Predicate predicate) const
Only features that match the given predicate.

◆ first()

std::optional< Feature > geodesk::Features::first ( ) const

Returns the first Feature in this collection, or std::nullopt if the collection is empty.

Only Nodes of a Way and member Features of a Relation are ordered; for all other collections, first() returns an arbitrary Feature.

Returns
the first Feature, or std::nullopt

◆ intersecting()

Features geodesk::Features::intersecting ( const Feature & feature) const

Only features whose geometry intersects with the given Feature.

Exceptions
QueryExceptionif one or more tiles that contain the geometry of a Relation are missing

◆ length()

double geodesk::Features::length ( ) const

Calculates the total length (in meters) of the features in this collection.

Exceptions
QueryExceptionif one or more tiles that contain the geometry of a Relation are missing

◆ maxMetersFrom() [1/2]

Features geodesk::Features::maxMetersFrom ( double distance,
Coordinate xy ) const

Only features whose closest point lies within distance meters of xy.

Parameters
distancethe maximum distance (in meters)
xythew center of the search radius

◆ maxMetersFrom() [2/2]

Features geodesk::Features::maxMetersFrom ( double distance,
double lon,
double lat ) const

Only features whose closest point lies within distance meters of the given location.

Parameters
distancethe maximum distance (in meters)
londegrees longitude of the center of the search radius
latdegrees latitude of the center of the search radius

◆ membersOf()

Features geodesk::Features::membersOf ( const Feature & feature) const

Only features that belong to the given Relation.

◆ nodes() [1/2]

Nodes geodesk::Features::nodes ( ) const

Only nodes.

◆ nodes() [2/2]

Nodes geodesk::Features::nodes ( const char * query)

Only nodes that match the given query.

Parameters
querya query in GOQL format
Exceptions
QueryExceptionif the query is malformed.

◆ nodesOf()

Features geodesk::Features::nodesOf ( const Feature & feature) const

Only nodes that belong to the given Way.

◆ one()

Feature geodesk::Features::one ( ) const

Returns the one and only Feature in this collection.

Returns
the sole Feature
Exceptions
QueryExceptionif this collection is empty or contains more than one Feature

◆ operator bool()

geodesk::Features::operator bool ( ) const

Returns true if this collection contains at least one Feature.

◆ operator!()

bool geodesk::Features::operator! ( ) const

Returns true if this collection is empty.

◆ operator()() [1/4]

Features geodesk::Features::operator() ( const Box & box) const

Only features whose bounding box intersects the given bounding box.

Parameters
box

◆ operator()() [2/4]

Features geodesk::Features::operator() ( const char * query) const

Only features that match the given query.

Parameters
querya query in GOQL format
Exceptions
QueryExceptionif the query is malformed.

◆ operator()() [3/4]

Features geodesk::Features::operator() ( const Feature & feature) const

Only features whose geometry intersects with the given Feature (short form of intersecting())

◆ operator()() [4/4]

Features geodesk::Features::operator() ( Coordinate xy) const

Only features whose bounding box contains the given Coordinate.

Parameters
xy

◆ parentsOf()

Features geodesk::Features::parentsOf ( const Feature & feature) const

Only features that are parent relations of the given Feature (or parent ways of the given Node).

◆ relations() [1/2]

Relations geodesk::Features::relations ( ) const

Only relations.

◆ relations() [2/2]

Relations geodesk::Features::relations ( const char * query) const

Only relations that match the given query.

Parameters
querya query in GOQL format
Exceptions
QueryExceptionif the query is malformed.

◆ store()

FeatureStore * geodesk::Features::store ( ) const
noexcept

Returns a pointer to the FeatureStore which contains the features in this collection.

◆ ways() [1/2]

Ways geodesk::Features::ways ( ) const

Only ways.

◆ ways() [2/2]

Ways geodesk::Features::ways ( const char * query) const

Only ways that match the given query.

Parameters
querya query in GOQL format
Exceptions
QueryExceptionif the query is malformed.

◆ within()

Features geodesk::Features::within ( const Feature & feature) const

Only features that lie entirely inside the geometry of the given Feature.

Exceptions
QueryExceptionif one or more tiles that contain the geometry of a Relation are missing

The documentation for this class was generated from the following file: