add parser for Sensor Observation Service GetFeatureOfInterest method, r=ahocevar (closes #2347)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@9911 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
bartvde
2009-12-21 14:31:32 +00:00
parent e99979ffe5
commit 2c23d9ff25
4 changed files with 274 additions and 0 deletions

View File

@@ -269,6 +269,7 @@
"OpenLayers/Format/SOSCapabilities.js",
"OpenLayers/Format/SOSCapabilities/v1_0_0.js",
"OpenLayers/Format/SOSGetObservation.js",
"OpenLayers/Format/SOSGetFeatureOfInterest.js",
"OpenLayers/Layer/WFS.js",
"OpenLayers/Control/GetFeature.js",
"OpenLayers/Control/MouseToolbar.js",

View File

@@ -0,0 +1,192 @@
/* Copyright (c) 2006-2009 MetaCarta, Inc., published under the Clear BSD
* license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
* full text of the license. */
/**
* @requires OpenLayers/Format/XML.js
* @requires OpenLayers/Format/GML/v3.js
*/
/**
* Class: OpenLayers.Format.SOSGetFeatureOfInterest
* Read and write SOS GetFeatureOfInterest. This is used to get to
* the location of the features (stations). The stations can have 1 or more
* sensors.
*
* Inherits from:
* - <OpenLayers.Format.XML>
*/
OpenLayers.Format.SOSGetFeatureOfInterest = OpenLayers.Class(
OpenLayers.Format.XML, {
/**
* Constant: VERSION
* {String} 1.0.0
*/
VERSION: "1.0.0",
/**
* Property: namespaces
* {Object} Mapping of namespace aliases to namespace URIs.
*/
namespaces: {
sos: "http://www.opengis.net/sos/1.0",
gml: "http://www.opengis.net/gml",
sa: "http://www.opengis.net/sampling/1.0",
xsi: "http://www.w3.org/2001/XMLSchema-instance"
},
/**
* Property: schemaLocation
* {String} Schema location
*/
schemaLocation: "http://www.opengis.net/sos/1.0 http://schemas.opengis.net/sos/1.0.0/sosAll.xsd",
/**
* Property: defaultPrefix
*/
defaultPrefix: "sos",
/**
* Property: regExes
* Compiled regular expressions for manipulating strings.
*/
regExes: {
trimSpace: (/^\s*|\s*$/g),
removeSpace: (/\s*/g),
splitSpace: (/\s+/),
trimComma: (/\s*,\s*/g)
},
/**
* Constructor: OpenLayers.Format.SOSGetFeatureOfInterest
*
* Parameters:
* options - {Object} An optional object whose properties will be set on
* this instance.
*/
initialize: function(options) {
OpenLayers.Format.XML.prototype.initialize.apply(this, [options]);
},
/**
* APIMethod: read
* Parse a GetFeatureOfInterest response and return an array of features
*
* Parameters:
* data - {String} or {DOMElement} data to read/parse.
*
* Returns:
* {Array(<OpenLayers.Feature.Vector>)} An array of features.
*/
read: function(data) {
if(typeof data == "string") {
data = OpenLayers.Format.XML.prototype.read.apply(this, [data]);
}
if(data && data.nodeType == 9) {
data = data.documentElement;
}
var info = {features: []};
this.readNode(data, info);
var features = [];
for (var i=0, len=info.features.length; i<len; i++) {
var container = info.features[i];
// reproject features if needed
if(this.internalProjection && this.externalProjection &&
container.components[0]) {
container.components[0].transform(
this.externalProjection, this.internalProjection
);
}
var feature = new OpenLayers.Feature.Vector(
container.components[0], container.attributes);
features.push(feature);
}
return features;
},
/**
* Property: readers
* Contains public functions, grouped by namespace prefix, that will
* be applied when a namespaced node is found matching the function
* name. The function will be applied in the scope of this parser
* with two arguments: the node being read and a context object passed
* from the parent.
*/
readers: {
"sa": {
"SamplingPoint": function(node, obj) {
// sampling point can also be without a featureMember if
// there is only 1
if (!obj.attributes) {
var feature = {attributes: {}};
obj.features.push(feature);
obj = feature;
}
obj.attributes.id = this.getAttributeNS(node,
this.namespaces.gml, "id");
this.readChildNodes(node, obj);
},
"position": function (node, obj) {
this.readChildNodes(node, obj);
}
},
"gml": OpenLayers.Util.applyDefaults({
"FeatureCollection": function(node, obj) {
this.readChildNodes(node, obj);
},
"featureMember": function(node, obj) {
var feature = {attributes: {}};
obj.features.push(feature);
this.readChildNodes(node, feature);
},
"name": function(node, obj) {
obj.attributes.name = this.getChildValue(node);
},
"pos": function(node, obj) {
// we need to parse the srsName to get to the
// externalProjection, that's why we cannot use
// GML v3 for this
if (!this.externalProjection) {
this.externalProjection = new OpenLayers.Projection(
node.getAttribute("srsName"));
}
OpenLayers.Format.GML.v3.prototype.readers.gml.pos.apply(
this, [node, obj]);
}
}, OpenLayers.Format.GML.v3.prototype.readers.gml)
},
/**
* Property: writers
* As a compliment to the readers property, this structure contains public
* writing functions grouped by namespace alias and named like the
* node names they produce.
*/
writers: {
"sos": {
"GetFeatureOfInterest": function(options) {
var node = this.createElementNSPlus("GetFeatureOfInterest", {
attributes: {
version: this.VERSION,
service: 'SOS',
"xsi:schemaLocation": this.schemaLocation
}
});
for (var i=0, len=options.fois.length; i<len; i++) {
this.writeNode("FeatureOfInterestId", {foi: options.fois[i]}, node);
}
return node;
},
"FeatureOfInterestId": function(options) {
var node = this.createElementNSPlus("FeatureOfInterestId", {value: options.foi});
return node;
}
}
},
CLASS_NAME: "OpenLayers.Format.SOSGetFeatureOfInterest"
});

View File

@@ -0,0 +1,80 @@
<html>
<head>
<script src="../../lib/OpenLayers.js"></script>
<script type="text/javascript">
function test_read_SOSGetFeatureOfInterest_single(t) {
t.plan(6);
var parser = new OpenLayers.Format.SOSGetFeatureOfInterest();
var text =
'<?xml version="1.0" encoding="UTF-8"?>' +
'<sa:SamplingPoint xmlns:sa="http://www.opengis.net/sampling/1.0" xmlns:gml="http://www.opengis.net/gml" gml:id="urn:ogc:object:feature:OSIRIS-HWS:4fc335bc-06d7-4d5e-a72a-1ac73b9f3b56">' +
'<gml:name>Roof of the IfGI</gml:name>' +
'<sa:position>' +
'<gml:Point>' +
'<gml:pos srsName="urn:ogc:def:crs:EPSG:4326">52.1524 5.3722</gml:pos>' +
'</gml:Point>' +
'</sa:position>' +
'</sa:SamplingPoint>';
var res = parser.read(text);
t.eq(res.length, 1, "One feature parsed from response");
t.eq(res[0].attributes.id, "urn:ogc:object:feature:OSIRIS-HWS:4fc335bc-06d7-4d5e-a72a-1ac73b9f3b56", "gml:id correctly parsed");
t.eq(res[0].attributes.name, "Roof of the IfGI", "gml:name correctly parsed");
t.eq(res[0].geometry instanceof OpenLayers.Geometry.Point, true, "Geometry is a point geometry");
t.eq(res[0].geometry.x, 5.3722, "Geometry x coordinate correctly parsed");
t.eq(res[0].geometry.y, 52.1524, "Geometry y coordinate correctly parsed");
}
function test_read_SOSGetFeatureOfInterest_multiple(t) {
t.plan(6);
var parser = new OpenLayers.Format.SOSGetFeatureOfInterest();
var text =
'<?xml version="1.0" encoding="UTF-8"?>' +
'<gml:FeatureCollection xmlns:gml="http://www.opengis.net/gml" xmlns:sa="http://www.opengis.net/sampling/1.0">' +
'<gml:featureMember>' +
'<sa:SamplingPoint gml:id="urn:ogc:object:feature:OSIRIS-HWS:3d3b239f-7696-4864-9d07-15447eae2b93">' +
'<gml:name>weather @ roof of the ifgi, MS, Germany</gml:name>' +
'<sa:position>' +
'<gml:Point>' +
'<gml:pos srsName="urn:ogc:def:crs:EPSG:4326">51.9412 7.6103</gml:pos>' +
'</gml:Point>' +
'</sa:position>' +
'</sa:SamplingPoint>' +
'</gml:featureMember>' +
'<gml:featureMember>' +
'<sa:SamplingPoint gml:id="urn:ogc:object:feature:OSIRIS-HWS:efeb807b-bd24-4128-a920-f6729bcdd111">' +
'<gml:name>waether @ roof of the FH Kaernten, Villach, Austria</gml:name>' +
'<sa:position>' +
'<gml:Point>' +
'<gml:pos srsName="urn:ogc:def:crs:EPSG:4326">46.611644 13.883498</gml:pos>' +
'</gml:Point>' +
'</sa:position>' +
'</sa:SamplingPoint>' +
'</gml:featureMember>' +
'</gml:FeatureCollection>';
var res = parser.read(text);
t.eq(res.length, 2, "Two features parsed from response");
t.eq(res[0].attributes.id, "urn:ogc:object:feature:OSIRIS-HWS:3d3b239f-7696-4864-9d07-15447eae2b93", "gml:id correctly parsed");
t.eq(res[1].attributes.name, "waether @ roof of the FH Kaernten, Villach, Austria", "gml:name correctly parsed");
t.eq(res[1].geometry instanceof OpenLayers.Geometry.Point, true, "Geometry is a point geometry");
t.eq(res[1].geometry.x, 13.883498, "Geometry x coordinate correctly parsed");
t.eq(res[1].geometry.y, 46.611644, "Geometry y coordinate correctly parsed");
}
function test_write_SOSGetFeatureOfInterest(t) {
t.plan(1);
var expect = '<GetFeatureOfInterest xmlns="http://www.opengis.net/sos/1.0" version="1.0.0" service="SOS" xsi:schemaLocation="http://www.opengis.net/sos/1.0 http://schemas.opengis.net/sos/1.0.0/sosAll.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><FeatureOfInterestId>urn:ogc:object:feature:OSIRIS-HWS:3d3b239f-7696-4864-9d07-15447eae2b93</FeatureOfInterestId><FeatureOfInterestId>urn:ogc:object:feature:OSIRIS-HWS:efeb807b-bd24-4128-a920-f6729bcdd111</FeatureOfInterestId></GetFeatureOfInterest>';
var format = new OpenLayers.Format.SOSGetFeatureOfInterest();
var output = format.writeNode("sos:GetFeatureOfInterest", {fois: ['urn:ogc:object:feature:OSIRIS-HWS:3d3b239f-7696-4864-9d07-15447eae2b93', 'urn:ogc:object:feature:OSIRIS-HWS:efeb807b-bd24-4128-a920-f6729bcdd111']});
t.xml_eq(output, expect, "Request XML is written out correctly");
}
</script>
</head>
<body>
</body>
</html>

View File

@@ -88,6 +88,7 @@
<li>Format/CSWGetRecords/v2_0_2.html</li>
<li>Format/SOSCapabilities/v1_0_0.html</li>
<li>Format/SOSGetObservation.html</li>
<li>Format/SOSGetFeatureOfInterest.html</li>
<li>Format/XML.html</li>
<li>Geometry.html</li>
<li>Geometry/Collection.html</li>