@wootapa/polygraph-ol

Extension of @wootapa/polygraph for OpenLayers.

Usage no npm install needed!

<script type="module">
  import wootapaPolygraphOl from 'https://cdn.skypack.dev/@wootapa/polygraph-ol';
</script>

README

Polygraph - OpenLayers

This is an extension of https://github.com/wootapa/polygraph with spatial operators and is intended to be used with Features or geometrylike objects. It also supports serialization into CQL/XML to be used by an OGC compliant server, such as Geoserver.

In short, features can be filtered directly in the client, or the server, using the same instance of Polygraph.

See demo with WFS/WMS side by side.

Installation

New browsers and bundlers (es):

$ npm install --save @wootapa/polygraph-ol

Old browsers (umd):

<script src="https://unpkg.com/@wootapa/polygraph-ol"></script>
// polygraph.and() ...

Methods

Below is only the spatial methods. See Polygraph library for the standard operators available for comparing ordinary attributes.

Statics

  • defaultProjection(projection) - Sets default projection for all new Polygraphs. The projection is assumed to be known by OpenLayers and values are assumed to be transformed. Defaults to EPSG:3857.

Spatial operators

value = geometrylike object. See below for what that is.

  • intersects(value) - True when object intersects value.
  • disjoint(value) - True when object do not intersects value.
  • contains(value) - True when object completely contains value.
  • within(value) - True when object is completely within value.
  • distanceWithin(value, distance, greatCircle?) - True when object is no more than specified distance (in meters) from value. Requires a correct projection. Uses greatCircle by default.
  • distanceBeyond(value, distance, greatCircle?) - True when object is more than specified distance (in meters) from value. Requires a correct projection. Uses greatCircle by default.

Other

  • projection(projection) - Overrides the default projection for current Polygraph.
  • asOgcCql(opts?) - Outputs as OGC CQL.
  • asOgcXML(opts?) - Outputs as OGC XML.

CQL/XML serializers take an optional object:

geometryName?, // Serializes operators with a different geometryName. Ex 'the_geom'.
projection?, // Serializes operators with a different projection. Ex 'EPSG:4326'.
decimals? // Rounds geometry decimal precision on serialized operators. Ex, 5.

Geometrylike objects

These types are all valid values for the spatial operators. If you need to evaluate ordinary attributes, use a type that can carry attributes.

  • ol/Feature (can carry attributes and respects geometryName)
  • ol/Geometry
  • An object with a valid ol/Geometry (ex feature.getProperties()) (can carry attributes)
  • WKT
  • GeoJSON (can carry attributes)
  • Array(2=point, 4=extent=polygon, 6=linestring, 8+=linestring/polygon)

An example

So maybe you have a bunch of features and you need all wells.

const q = and().eq('type', 'well').done();

You figure the depth must at least 32 meters

q.gte('depth', 32).done()

It also must be drilled before 1998

q.lte('drilled', new Date(1998,0)).done()

It should also intersect the area of interest

q.intersects([13.8517, 55.9646, 14.3049, 56.1017]).done() // <- You have options what you pass here.

In the end, this is the result.

const q = and()
    .eq('type', 'well')
    .gte('depth', 32)
    .lte('drilled', new Date(1998,0))
    .intersects([13.8517, 55.9646, 14.3049, 56.1017])
    .done();

Apply on client features...

const features = [...];
const wells = features.filter(q.evaluate);

...or output as CQL/XML and pass it to your OGC compliant server.

const opts = { geometryName: 'geom', projection: 'EPSG:3006', decimals: 0 }; // <- Optional
const cql = q.asOgcCql(opts);
const xml = q.asOgcXml(opts);

Pro ol-tip!

To hide/show features based on the result you can do:

const hiddenStyle = new Style();
source.forEachFeature(feature => {
    feature.setStyle(
        q.evaluate(feature)
            ? null        // visible (use layer style)
            : hiddenStyle // hidden (overrides layer style)
        );
});