Skip to content

Aggregate multiple features

alexander-petkov edited this page Jun 20, 2022 · 2 revisions

Aggregate functions for multiple features

I wrote a wrapper for the gs:Aggregate WPS that generates Aggregate summaries for 1 or more features.

Input agruments needed (at least 1, equal # of each):

  1. features
  2. function
  3. aggregationAttribute

Output format:

  1. text/xml
  2. application/json

Example HTTP Get:

http://localhost:8080/geoserver/wps?SERVICE=WPS
   &REQUEST=Execute
   &Identifier=gs:AggregateMultiple
   &DataInputs=features=@xlink:href=http%3A%2F%2Fgeoserver%2Fwfs%3FVersion%3D1.0.0%26typeName%3Dtopp%3Astates;
   aggregationAttribute=STATE_NAME;
   function=Count;
   features=@xlink:href=http%3A%2F%2Fgeoserver%2Fwfs%3FVersion%3D1.0.0%26typeName%3Dtest%3AMODIS_C6_Global_24h;
   function=Average;
   aggregationAttribute=FRP
   &Rawdataoutput=result=@mimetype=text/xml

Example HTTP Post

<?xml version="1.0" encoding="UTF-8"?><wps:Execute version="1.0.0" service="WPS" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.opengis.net/wps/1.0.0" xmlns:wfs="http://www.opengis.net/wfs" xmlns:wps="http://www.opengis.net/wps/1.0.0" xmlns:ows="http://www.opengis.net/ows/1.1" xmlns:gml="http://www.opengis.net/gml" xmlns:ogc="http://www.opengis.net/ogc" xmlns:wcs="http://www.opengis.net/wcs/1.1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xsi:schemaLocation="http://www.opengis.net/wps/1.0.0 http://schemas.opengis.net/wps/1.0.0/wpsAll.xsd">
  <ows:Identifier>gs:AggregateMultiple</ows:Identifier>
  <wps:DataInputs>
    <wps:Input>
      <ows:Identifier>features</ows:Identifier>
      <wps:Reference mimeType="text/xml" xlink:href="http://geoserver/wfs" method="POST">
        <wps:Body>
          <wfs:GetFeature service="WFS" version="1.0.0" outputFormat="GML2" xmlns:test="http://test.org">
            <wfs:Query typeName="test:MODIS_C6_Global_24h"/>
          </wfs:GetFeature>
        </wps:Body>
      </wps:Reference>
    </wps:Input>
    <wps:Input>
      <ows:Identifier>features</ows:Identifier>
      <wps:Reference mimeType="text/xml" xlink:href="http://geoserver/wfs" method="POST">
        <wps:Body>
          <wfs:GetFeature service="WFS" version="1.0.0" outputFormat="GML2" xmlns:topp="http://www.openplans.org/topp">
            <wfs:Query typeName="topp:states"/>
          </wfs:GetFeature>
        </wps:Body>
      </wps:Reference>
    </wps:Input>
    <wps:Input>
      <ows:Identifier>aggregationAttribute</ows:Identifier>
      <wps:Data>
        <wps:LiteralData>FRP</wps:LiteralData>
      </wps:Data>
    </wps:Input>
   <wps:Input>
      <ows:Identifier>aggregationAttribute</ows:Identifier>
      <wps:Data>
        <wps:LiteralData>STATE_NAME</wps:LiteralData>
      </wps:Data>
    </wps:Input>
   <wps:Input>
      <ows:Identifier>function</ows:Identifier>
      <wps:Data>
        <wps:LiteralData>Average</wps:LiteralData>
      </wps:Data>
    </wps:Input>
    <wps:Input>
      <ows:Identifier>function</ows:Identifier>
      <wps:Data>
        <wps:LiteralData>Count</wps:LiteralData>
      </wps:Data>
    </wps:Input>
    <wps:Input>
      <ows:Identifier>singlePass</ows:Identifier>
      <wps:Data>
        <wps:LiteralData>false</wps:LiteralData>
      </wps:Data>
    </wps:Input>
  </wps:DataInputs>
  <wps:ResponseForm>
    <wps:RawDataOutput mimeType="text/xml">
      <ows:Identifier>result</ows:Identifier>
    </wps:RawDataOutput>
  </wps:ResponseForm>
</wps:Execute>

Result:

<list>
<AggregationResults>
<Count>49</Count>
</AggregationResults>
<AggregationResults>
<Average>90.89841123854774</Average>
</AggregationResults>
</list>

Building count and total population within a geometry

Example HTTP Get (URL encoded)

curl --insecure -X GET \
   "https://aws.wfas.net/geoserver/wps?
   SERVICE=WPS&REQUEST=Execute
   &Identifier=gs:AggregateMultiple
   &DataInputs=features=@xlink:href=http%3A%2F%2Fgeoserver%2Fwfs%3FVersion%3D1.0.0%26typeName=bing:usbuildings%26cql_filter%3DINTERSECTS(geom%2CPOLYGON((-114.049851%2046.895869%2C%20-114.043593%2046.895869%2C%20-114.043593%2046.891552%2C%20-114.049851%2046.891552%2C%20-114.049851%2046.895869)))
   ;function=Count
   ;aggregationAttribute=fid
   ;features=@xlink:href=http%3A%2F%2Fgeoserver%2Fwfs%3FVersion%3D1.0.0%26typeName=wfas:population%26cql_filter%3DINTERSECTS(wkb_geometry%2CPOLYGON((-114.049851%2046.895869%2C%20-114.043593%2046.895869%2C%20-114.043593%2046.891552%2C%20-114.049851%2046.891552%2C%20-114.049851%2046.895869)))
   ;function=Sum
   ;aggregationAttribute=grid_code
   &Rawdataoutput=result=@mimetype=application/json"

Example HTTP Post:

curl --insecure -H "Content-type: xml" -X POST \
   -d@aggregation_multiple.xml \ 
  https://aws.wfas.net/geoserver/wps -o response.json

aggregation_multiple.xml contents:

<?xml version="1.0" encoding="UTF-8"?><wps:Execute version="1.0.0" service="WPS" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.opengis.net/wps/1.0.0" xmlns:wfs="http://www.opengis.net/wfs" xmlns:wps="http://www.opengis.net/wps/1.0.0" xmlns:ows="http://www.opengis.net/ows/1.1" xmlns:gml="http://www.opengis.net/gml" xmlns:ogc="http://www.opengis.net/ogc" xmlns:wcs="http://www.opengis.net/wcs/1.1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xsi:schemaLocation="http://www.opengis.net/wps/1.0.0 http://schemas.opengis.net/wps/1.0.0/wpsAll.xsd">
  <ows:Identifier>gs:AggregateMultiple</ows:Identifier>
  <wps:DataInputs>
    <wps:Input>
      <ows:Identifier>features</ows:Identifier>
      <wps:Reference mimeType="text/xml" xlink:href="http://geoserver/wfs" method="POST">
        <wps:Body>
          <wfs:GetFeature service="WFS" version="1.0.0" outputFormat="GML2" xmlns:bing="http://bing.com">
            <wfs:Query typeName="bing:usbuildings">
            <ogc:Filter>
              <ogc:Intersects>
                <ogc:PropertyName>geom</ogc:PropertyName>
                <Literal>
                  <gml:Polygon srsName="http://www.opengis.net/gml/srs/epsg.xml#4326">
                    <gml:exterior>
                      <gml:LinearRing>
                        <gml:posList>-114.049851 46.895869 -114.043593 46.895869 -114.043593 46.891552 -114.049851 46.891552 -114.049851 46.895869</gml:posList>
                      </gml:LinearRing>
                    </gml:exterior>
                  </gml:Polygon>
                </Literal>
              </ogc:Intersects>
            </ogc:Filter>
          </wfs:Query>
          </wfs:GetFeature>
        </wps:Body>
      </wps:Reference>
    </wps:Input>
    <wps:Input>
      <ows:Identifier>features</ows:Identifier>
      <wps:Reference mimeType="text/xml" xlink:href="http://geoserver/wfs" method="POST">
        <wps:Body>
          <wfs:GetFeature service="WFS" version="1.0.0" outputFormat="GML2" xmlns:wfas="https://www.wfas.net">
            <wfs:Query typeName="wfas:population">
            <ogc:Filter>
              <ogc:Intersects>
                <ogc:PropertyName>wkb_geometry</ogc:PropertyName>
                <Literal>
                  <gml:Polygon srsName="http://www.opengis.net/gml/srs/epsg.xml#4326">
                    <gml:exterior>
                      <gml:LinearRing>
                        <gml:posList>-114.049851 46.895869 -114.043593 46.895869 -114.043593 46.891552 -114.049851 46.891552 -114.049851 46.895869</gml:posList>
                      </gml:LinearRing>
                    </gml:exterior>
                  </gml:Polygon>
                </Literal>
              </ogc:Intersects>
            </ogc:Filter>
          </wfs:Query>
          </wfs:GetFeature>
        </wps:Body>
      </wps:Reference>
    </wps:Input>
    <wps:Input>
      <ows:Identifier>aggregationAttribute</ows:Identifier>
      <wps:Data>
        <wps:LiteralData>fid</wps:LiteralData>
      </wps:Data>
    </wps:Input>
   <wps:Input>
      <ows:Identifier>aggregationAttribute</ows:Identifier>
      <wps:Data>
        <wps:LiteralData>grid_code</wps:LiteralData>
      </wps:Data>
    </wps:Input>
   <wps:Input>
      <ows:Identifier>function</ows:Identifier>
      <wps:Data>
        <wps:LiteralData>Count</wps:LiteralData>
      </wps:Data>
    </wps:Input>
    <wps:Input>
      <ows:Identifier>function</ows:Identifier>
      <wps:Data>
        <wps:LiteralData>Sum</wps:LiteralData>
      </wps:Data>
    </wps:Input>
    <wps:Input>
      <ows:Identifier>singlePass</ows:Identifier>
      <wps:Data>
        <wps:LiteralData>false</wps:LiteralData>
      </wps:Data>
    </wps:Input>
  </wps:DataInputs>
  <wps:ResponseForm>
    <wps:RawDataOutput mimeType="application/json">
      <ows:Identifier>result</ows:Identifier>
    </wps:RawDataOutput>
  </wps:ResponseForm>
</wps:Execute>

Result:

[
 {"GroupByAttributes":[],
   "AggregationResults":[[159]],
   "AggregationFunctions":["Count"],
   "AggregationAttribute":"fid"},
 {"GroupByAttributes":[],
   "AggregationResults":[[636]],
   "AggregationFunctions":["Sum"],
   "AggregationAttribute":"grid_code"}
]
<list>
 <AggregationResults>
   <Count>159</Count>
 </AggregationResults>
 <AggregationResults>
   <Sum>636.0</Sum>
 </AggregationResults>
</list>