Monday 23 March 2015

openlayers 2 - Check if property exists on features in layer


I was wondering if there is a good way to check to see if a property exists on a particular feature layer in GeoServer? My use case is that I have a bunch of features I want to display on a map. Some of the feature layers will have a date property associated with them. If they have a date property I want to be able to display a control to filter the features shown by date. I noticed that WFS has an operation called GetPropertyValue which will return an exception if the property doesn't exist. However, if it does it (as expected) returns the value for that property for all features which can be very slow. My general algorithm is as follows:



var wfsLayer = new OpenLayers.Layer.Vector('MyVectorLayer', {
strategies: [new Openlayers.Strategy.BBOX()],
protocol: new OpenLayers.Protocol.WFS({
url: 'http://mygeoserver/geoserver/wfs',
featureType: 'MyVectorLayer',
})
});

// if layer has date property (this is the part I need help with)
var dateFilter = new OpenLayers.Filter.Comparison({

type: OpenLayers.Filter.Comparison.BETWEEN,
property: 'date',
lowerBoundary: startDateVal,
upperBoundary: endDateVal
});
wfsLayer.filter = dateFilter;
// end if

Answer



You can utilize the WFS service requests DescribeFeatureType to identify all the attributes and their types for a specific typeName. This is probably the most appropriate way to identify whether there is a datetime field on your layer of interest. There is no existing WFS DescribeFeatureType example for OpenLayers, but I slightly modified the WMS DescribeLayer example to show how you could accomplish this.


Here is the HTML/JS example:









OpenLayers WFSDescribeFeatureType Parser Example





WFSDescribeFeatureType Parser Example




wfsdescribefeaturetype, parser, cleanup




Demonstrate the operation of the WFSDescribeFeatureType parser.






This script reads data from a file and parses out attributes on a typeName,

appending them to a HTML string with markup tags. This markup is
dumped to an element in the page.






Here is also the xml file that I used as input:



































I placed my html in the openlayers/examples directory of the source tree and the xml in openlayers/examples/xml directory. I then just served it up using apache.


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...