[Geoserver-users] Joining and Filtering

I have a question that sort of extends from the thread titled "Joining tables
ub WFS ?"

That discussion ended up saying you can't do queries that involve Join
operations. It offered the solution of defining a View in PostGRE that
represents the Join. Then register the view with GeoServer, and query the
view using GetRequest "as usual."

So... I'm pretty new at writing requests and using filters. I'll give an
example of the type of query I want to invoke through WFS. Lets say I have a
PostGIS table, us_states, that contains the boundaries of the US States in a
field named the_geom, and the two-letter postal abbreviations in a field
named state_abbr. I want to get the state abbreviations of all states that
border another state, say NY. The PostGRE spatial query for this (taking
advantage of spatial queries) would look something like this:

SELECT a.state_abbr FROM us_states as a, us_states as b WHERE b.state_abbr =
'NY' AND a.the_geom && b.the_geom AND DISTANCE(a.the_geom, b.the_geom) = 0;

Assume I have a view defined for the self-join as follows:

CREATE VIEW us_states_join_us_states AS SELECT a.state_abbr AS a_abbr,
a.the_geom AS a_geom, b.the_geom AS b_geom, b.state_abbr AS b_abbr FROM
us_states a, us_states b;

How would I make a GetRequest with a Filter that would have the effect of
the query I wrote above? I was thinking it would be something like using the
<ogc:Touches> filter, but I haven't figured out how to use it with two
geometry fields in the view... Could anyone help me with an explicit example
of what the GetRequest looks like for the query above?

Kind Regards,
Victor
--
View this message in context: http://www.nabble.com/Joining-and-Filtering-tf4952678.html#a14182071
Sent from the GeoServer - User mailing list archive at Nabble.com.

Hi Victor,

I do not believe it is possible to define a distance filter (or any of
the spatial filters) in which both operands are geometry properties.

From the filter schema:

<xsd:complexType name="DistanceBufferType">
<xsd:complexContent>
  <xsd:extension base="ogc:SpatialOpsType">
   <xsd:sequence>
    <xsd:element ref="ogc:PropertyName"/>
    <xsd:element ref="gml:_Geometry"/>
    <xsd:element name="Distance" type="ogc:DistanceType"/>
   </xsd:sequence>
  </xsd:extension>
</xsd:complexContent>
</xsd:complexType>

Which basically means that a binary spatial operators must be made up of
a property, and a geometry "literal".

However... the internal filter object model might allow the following.
However if XML validation is turned on you will get an error. But it
might be worth trying the following and seeing what happens:

<Filter>
  <And>
     <PropertyIsEqualTo>
        <PropertyName>a_abbr</PropertyName>
        <Literal>NY</Literal>
     </PropertyIsEqualTo>
     <DWithin>
        <PropertyName>a_geom</PropertyName>
        <PropertyName>b_geom</PropertyName>
        <Distance>0</Distance>
     <DWithin>
  </And>
</Filter>

Let us know how it goes.

-Justin

SELECT a.state_abbr FROM us_states as a, us_states as b WHERE b.state_abbr =
'NY' AND a.the_geom && b.the_geom AND DISTANCE(a.the_geom, b.the_geom) = 0;

Assume I have a view defined for the self-join as follows:

CREATE VIEW us_states_join_us_states AS SELECT a.state_abbr AS a_abbr,
a.the_geom AS a_geom, b.the_geom AS b_geom, b.state_abbr AS b_abbr FROM
us_states a, us_states b;

How would I make a GetRequest with a Filter that would have the effect of
the query I wrote above? I was thinking it would be something like using the
<ogc:Touches> filter, but I haven't figured out how to use it with two
geometry fields in the view... Could anyone help me with an explicit example
of what the GetRequest looks like for the query above?

Kind Regards,
Victor

--
Justin Deoliveira
The Open Planning Project
http://topp.openplans.org