Saturday, 14 October 2017

Geoserver WMS Filtering: Clipping geometry against a polygon



My data set is a series of multilinestrings representing roads. During a WMS GetMap request, I want to specify a polygon and display only the portions of those roads that fall within the polygon. I am fine using ECQL or SLD.


My current CQL_FILTER looks like:


INTERSECTS(wkb_geometry, POLYGON((-117 34 ... ...)))

This returns all roads intersecting the polygon, but displays the full extent of those roads - i.e. it displays all vertices/segments for the entire road, not just those contained within the polygon. Image example:


enter image description here


Is there a way to extract the vertices from each geometry, and filter against those individual vertices, instead of the entire line?


Something like the following command, although this doesn't work,


INTERSECTS(VERTICES(wkb_geometry), POLYGON((... ...))))


Failing with the error message:



Rendering process failed Binary geometry filter, but first expression is not a property name? (it's a class org.geotools.filter.function.FilterFunction_vertices)



This makes sense, as VERTICES(wkb_geometry) doesn't actually return a property of the dataset, but an entirely new set of data. I assume this is called "geometry transformation", but I can't figure out whether it even CAN be done in this way, and if so, how.



Answer



Figured it out!


enter image description here


The following SLD uses a rendering transformation to call "gs:Clip", which clips vector features to a bounding geometry. The result is then rasterized by GeoServer's WMS.




xmlns:ogc="http://www.opengis.net/ows"
xmlns:gml="http://www.opengis.net/gml">

as15:placemarks_line






features


clip
POLYGON((
-118.0 33.8,
-118.2 33.8,
-118.2 34.0,
-118.0 34.0,
-118.0 33.8

))







#0000FF
3









Inline
1










-118.0,33.8 -118.2,33.8 -118.2,34.0 -118.0,34.0 -118.0,33.8

















#FF0000
2









Note that everything after the first NamedLayer element is optional - it's simply drawing the bounding polygon to help visualize the results.


Haven't figured out how to dynamically specify the bounding polygon yet, although it's likely possible via feature definitions or SLD variable substitution.


Also, haven't figured out how to specify an existing style - i.e. when the filter passes, use an existing style (the default, actually), so for now, the roads are simply drawn in a blue style. I'm going to post another question asking about that.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...