| 1 | package Geo::OGC::Service::Filter; | ||
| 2 | |||
| 3 | use Modern::Perl; | ||
| 4 | use Carp; | ||
| 5 | use Data::Dumper; | ||
| 6 | use Geo::OGC::Service; | ||
| 7 | use vars qw(@ISA); | ||
| 8 | push @ISA, qw(Geo::OGC::Service::Common); | ||
| 9 | |||
| 10 | require Exporter; | ||
| 11 | push @ISA, qw(Exporter); | ||
| 12 | our @EXPORT_OK = qw(filter2sql node2sql parse_tag parse_name strip get_integer latest_version); | ||
| 13 | our %EXPORT_TAGS = (all => \@EXPORT_OK); | ||
| 14 | |||
| 15 | our %gml_geometry_type = ( | ||
| 16 | Envelope => 1, | ||
| 17 | Point => 1, | ||
| 18 | MultiPoint => 1, | ||
| 19 | LineString => 1, | ||
| 20 | MultiLineString => 1, | ||
| 21 | Polygon => 1, | ||
| 22 | MultiPolygon => 1, | ||
| 23 | MultiGeometry => 1, | ||
| 24 | ); | ||
| 25 | |||
| 26 | our %spatial2op = ( | ||
| 27 | Disjoint => 1, | ||
| 28 | Equals => 1, | ||
| 29 | Touches => 1, | ||
| 30 | Within => 1, | ||
| 31 | Overlaps => 1, | ||
| 32 | Crosses => 1, | ||
| 33 | Intersects => 1, | ||
| 34 | Contains => 1 | ||
| 35 | ); # there are more in PostGIS | ||
| 36 | |||
| 37 | our %temporal_operators = ( | ||
| 38 | After => 1, | ||
| 39 | Before => 1, | ||
| 40 | Begins => 1, | ||
| 41 | BegunBy => 1, | ||
| 42 | TContains => 1, | ||
| 43 | During => 1, | ||
| 44 | TEquals => 1, | ||
| 45 | TOverlaps => 1, | ||
| 46 | Meets => 1, | ||
| 47 | OverlappedBy => 1, | ||
| 48 | MetBy => 1, | ||
| 49 | EndedBy => 1 | ||
| 50 | ); | ||
| 51 | |||
| 52 | my %functions = ( | ||
| 53 | abs => ['xs:int', [[int => 'xs:int']]], | ||
| 54 | acos => ['xs:double', [[value => 'xs:double']]], | ||
| 55 | Area => ['xs:double', [[geometry => 'gml:AbstractGeometryType']]], | ||
| 56 | within => ['xs:boolean', [[geometry => 'gml:AbstractGeometryType'], [geometry => 'gml:AbstractGeometryType']]] | ||
| 57 | ); # lots missing | ||
| 58 | |||
| 59 | sub Filter_Capabilities { | ||
| 60 | my ($self, $writer) = @_; | ||
| 61 | my $ns = $self->{version} eq '2.0.0' ? 'fes' : 'ogc'; | ||
| 62 | $writer->open_element($ns.':Filter_Capabilities'); | ||
| 63 | |||
| 64 | # Conformance | ||
| 65 | my %Constraints = ( | ||
| 66 | ImplementsQuery => [['ows:NoValues'], ['ows:DefaultValue' => 'TRUE']], | ||
| 67 | ImplementsAdHocQuery => [['ows:NoValues'], ['ows:DefaultValue' => 'TRUE']], | ||
| 68 | ImplementsFunctions => [['ows:NoValues'], ['ows:DefaultValue' => 'TRUE']], | ||
| 69 | ImplementsMinStandardFilter => [['ows:NoValues'], ['ows:DefaultValue' => 'TRUE']], | ||
| 70 | ImplementsStandardFilter => [['ows:NoValues'], ['ows:DefaultValue' => 'FALSE']], | ||
| 71 | ImplementsMinSpatialFilter => [['ows:NoValues'], ['ows:DefaultValue' => 'TRUE']], | ||
| 72 | ImplementsSpatialFilter => [['ows:NoValues'], ['ows:DefaultValue' => 'FALSE']], | ||
| 73 | ImplementsMinTemporalFilter => [['ows:NoValues'], ['ows:DefaultValue' => 'TRUE']], | ||
| 74 | ImplementsTemporalFilter => [['ows:NoValues'], ['ows:DefaultValue' => 'TRUE']], | ||
| 75 | ImplementsVersionNav => [['ows:NoValues'], ['ows:DefaultValue' => 'FALSE']], | ||
| 76 | ImplementsSorting => [['ows:AllowedValues' => [['ows:Value' => 'ASC'], ['ows:Value' => 'DESC']]], | ||
| 77 | ['ows:DefaultValue' => 'ASC']], | ||
| 78 | ImplementsExtendedOperators => [['ows:NoValues'], ['ows:DefaultValue' => 'FALSE']], | ||
| 79 | ); | ||
| 80 | my @c; | ||
| 81 | for my $key (keys %Constraints) { | ||
| 82 | push @c, [$ns.':Constraint', {name=>$key}, $Constraints{$key}]; | ||
| 83 | } | ||
| 84 | $writer->element($ns.':Conformance', \@c); | ||
| 85 | |||
| 86 | my @ids; | ||
| 87 | if ($ns eq 'ogc') { | ||
| 88 | @ids = ([$ns.':FID']); | ||
| 89 | } else { | ||
| 90 | @ids = (['fes:ResourceIdentifier', {name => 'fes:ResourceId'}]); | ||
| 91 | } | ||
| 92 | $writer->element($ns.':Id_Capabilities', \@ids); | ||
| 93 | |||
| 94 | my @operators = (); | ||
| 95 | for my $o (qw/LessThan GreaterThan LessThanOrEqualTo GreaterThanOrEqualTo EqualTo NotEqualTo Like Between Null/) { | ||
| 96 | if ($ns eq 'ogc') { | ||
| 97 | push @operators, [$ns.':ComparisonOperator', 'PropertyIs'.$o]; | ||
| 98 | } else { | ||
| 99 | push @operators, [$ns.':ComparisonOperator', { name => 'PropertyIs'.$o}]; | ||
| 100 | } | ||
| 101 | } | ||
| 102 | $writer->element($ns.':Scalar_Capabilities', | ||
| 103 | [[$ns.':LogicalOperators'], # empty ? | ||
| 104 | [$ns.':ComparisonOperators', \@operators]]); | ||
| 105 | |||
| 106 | my @operands = (); | ||
| 107 | for my $o (keys %gml_geometry_type) { | ||
| 108 | if ($ns eq 'ogc') { | ||
| 109 | push @operands, [$ns.':GeometryOperand', 'gml:'.$o]; | ||
| 110 | } else { | ||
| 111 | push @operands, [$ns.':GeometryOperand', { name => 'gml:'.$o }]; | ||
| 112 | } | ||
| 113 | } | ||
| 114 | @operators = (); | ||
| 115 | my @op = keys %spatial2op; | ||
| 116 | push @op, (qw/DWithin BBOX/); | ||
| 117 | for my $o (@op) { | ||
| 118 | push @operators, [$ns.':SpatialOperator', { name => $o }]; | ||
| 119 | } | ||
| 120 | $writer->element($ns.':Spatial_Capabilities', | ||
| 121 | [[$ns.':GeometryOperands', \@operands], | ||
| 122 | [$ns.':SpatialOperators', \@operators]]); | ||
| 123 | |||
| 124 | # Temporal_Capabilities | ||
| 125 | |||
| 126 | @operands = (); | ||
| 127 | for my $o (qw/TimeInstant TimePeriod/) { | ||
| 128 | if ($ns eq 'ogc') { | ||
| 129 | push @operands, [$ns.':GeometryOperand', 'gml:'.$o]; | ||
| 130 | } else { | ||
| 131 | push @operands, [$ns.':GeometryOperand', { name => 'gml:'.$o }]; | ||
| 132 | } | ||
| 133 | } | ||
| 134 | @operators = (); | ||
| 135 | @op = keys %temporal_operators; | ||
| 136 | for my $o (@op) { | ||
| 137 | push @operators, [$ns.':TemporalOperator', { name => $o }]; | ||
| 138 | } | ||
| 139 | $writer->element($ns.':Temporal_Capabilities', | ||
| 140 | [[$ns.':TemporalOperands', \@operands], | ||
| 141 | [$ns.':TemporalOperators', \@operators]]); | ||
| 142 | |||
| 143 | # Functions | ||
| 144 | |||
| 145 | my @functions; | ||
| 146 | for my $f (sort keys %functions) { | ||
| 147 | my @args = (); | ||
| 148 | my $args = $functions{$f}[1]; | ||
| 149 | for my $arg (@$args) { | ||
| 150 | push @args, [$ns.':Argument', { name => $arg->[0]}, [$ns.':Type' => $arg->[1]]]; | ||
| 151 | } | ||
| 152 | push @functions, [$ns.':Function', { name => $f }, | ||
| 153 | [[$ns.':Returns' => $functions{$f}[0]] ,[$ns.':Arguments' => \@args]]]; | ||
| 154 | } | ||
| 155 | $writer->element($ns.':Functions', \@functions); | ||
| 156 | |||
| 157 | $writer->close_element; | ||
| 158 | } | ||
| 159 | |||
| 160 | sub error { | ||
| 161 | my ($self, $msg, $headers) = @_; | ||
| 162 | if (!$msg->{debug}) { | ||
| 163 | Geo::OGC::Service::error($self->{responder}, $msg, $headers); | ||
| 164 | } else { | ||
| 165 | print STDERR Dumper $msg; | ||
| 166 | } | ||
| 167 | } | ||
| 168 | |||
| 169 | sub log { | ||
| 170 | my ($self, $msg) = @_; | ||
| 171 | say STDERR Dumper($msg); | ||
| 172 | } | ||
| 173 | |||
| 174 | # convert OGC Filter XML to SQL | ||
| 175 | sub filter2sql { | ||
| 176 | my ($node, $type) = @_; | ||
| 177 | if (!$node) { | ||
| 178 | return ""; | ||
| 179 | } | ||
| 180 | my ($ns, $name) = parse_tag($node); | ||
| 181 | |||
| 182 | if ($name eq 'Literal') { | ||
| 183 | my $child = $node->firstChild; | ||
| 184 | my $data = $child ? $child->data : ''; | ||
| 185 | return "'".$data."'"; | ||
| 186 | |||
| 187 | } elsif ($name eq 'PropertyName') { | ||
| 188 | my $ref = strip($node->textContent); | ||
| 189 | $ref = $type->{GeometryColumn} if $ref eq 'geometryProperty'; | ||
| 190 | return '"'.$ref.'"'; | ||
| 191 | |||
| 192 | } elsif ($name eq 'ValueReference') { | ||
| 193 | my $ref = strip($node->textContent); | ||
| 194 | $ref =~ s/^\w+://; | ||
| 195 | $ref = $type->{GeometryColumn} if $ref eq 'geometryProperty'; | ||
| 196 | return '"'.$ref.'"'; | ||
| 197 | |||
| 198 | } elsif ($name eq 'ResourceId') { | ||
| 199 | my $id = $node->getAttribute('rid'); | ||
| 200 | return $type->{'gml:id'}." = '$id'"; | ||
| 201 | |||
| 202 | } elsif ($name eq 'FeatureId') { | ||
| 203 | my $id = $node->getAttribute('fid'); | ||
| 204 | return $type->{'gml:id'}." = '$id'"; | ||
| 205 | |||
| 206 | } elsif ($name eq 'GmlObjectId') { | ||
| 207 | my $id = $node->getAttribute('gml:id'); | ||
| 208 | return $type->{'gml:id'}." = '$id'"; | ||
| 209 | |||
| 210 | } elsif ($name eq 'PropertyIsEqualTo') { | ||
| 211 | $node = $node->firstChild; | ||
| 212 | return '('.filter2sql($node, $type).' = '.filter2sql($node->nextSibling, $type).')'; | ||
| 213 | |||
| 214 | } elsif ($name eq 'PropertyIsNotEqualTo') { | ||
| 215 | $node = $node->firstChild; | ||
| 216 | return '('.filter2sql($node, $type).' != '.filter2sql($node->nextSibling, $type).')'; | ||
| 217 | |||
| 218 | } elsif ($name eq 'PropertyIsLessThan') { | ||
| 219 | $node = $node->firstChild; | ||
| 220 | return '('.filter2sql($node, $type).' < '.filter2sql($node->nextSibling, $type).')'; | ||
| 221 | |||
| 222 | } elsif ($name eq 'PropertyIsGreaterThan') { | ||
| 223 | $node = $node->firstChild; | ||
| 224 | return '('.filter2sql($node, $type).' > '.filter2sql($node->nextSibling, $type).')'; | ||
| 225 | |||
| 226 | } elsif ($name eq 'PropertyIsLessThanOrEqualTo') { | ||
| 227 | $node = $node->firstChild; | ||
| 228 | return '('.filter2sql($node, $type).' <= '.filter2sql($node->nextSibling, $type).')'; | ||
| 229 | |||
| 230 | } elsif ($name eq 'PropertyIsGreaterThanOrEqualTo') { | ||
| 231 | $node = $node->firstChild; | ||
| 232 | return '('.filter2sql($node, $type).' >= '.filter2sql($node->nextSibling, $type).')'; | ||
| 233 | |||
| 234 | } elsif ($name eq 'PropertyIsBetween') { | ||
| 235 | $node = $node->firstChild; | ||
| 236 | my $property = filter2sql($node->firstChild, $type); | ||
| 237 | $node = $node->nextSibling; | ||
| 238 | return '('.$property.' >= '.filter2sql($node, $type).' AND '.$property.'<='.filter2sql($node->nextSibling).')'; | ||
| 239 | |||
| 240 | } elsif ($name eq 'PropertyIsLike') { | ||
| 241 | $node = $node->firstChild; | ||
| 242 | return '('.filter2sql($node, $type).' ~ '.filter2sql($node->nextSibling, $type).')'; | ||
| 243 | |||
| 244 | } elsif ($name eq 'PropertyIsNull') { | ||
| 245 | return '('.filter2sql($node->firstChild, $type).' ISNULL)'; | ||
| 246 | |||
| 247 | } elsif ($name eq 'And') { | ||
| 248 | $node = $node->firstChild; | ||
| 249 | my $p = '('.filter2sql($node, $type); | ||
| 250 | while ($node = $node->nextSibling) { | ||
| 251 | $p .= ' AND '.filter2sql($node, $type); | ||
| 252 | } | ||
| 253 | return $p.')'; | ||
| 254 | |||
| 255 | } elsif ($name eq 'Or') { | ||
| 256 | $node = $node->firstChild; | ||
| 257 | my $p = '('.filter2sql($node, $type); | ||
| 258 | while ($node = $node->nextSibling) { | ||
| 259 | $p .= ' OR '.filter2sql($node, $type); | ||
| 260 | } | ||
| 261 | return $p.')'; | ||
| 262 | } elsif ($name eq 'Not') { | ||
| 263 | return '(NOT '.filter2sql($node->firstChild, $type).')'; | ||
| 264 | |||
| 265 | } elsif ($ns eq 'gml' and $gml_geometry_type{$name}) { | ||
| 266 | return node2sql($node); | ||
| 267 | |||
| 268 | } elsif ($name eq 'BBOX') { | ||
| 269 | $node = $node->firstChild; | ||
| 270 | my ($ns, $n) = parse_tag($node); | ||
| 271 | my $property; | ||
| 272 | if ($n eq 'Envelope') { | ||
| 273 | $property = 'GeometryColumn'; | ||
| 274 | } else { | ||
| 275 | $property = filter2sql($node, $type); | ||
| 276 | $node = $node->nextSibling; | ||
| 277 | } | ||
| 278 | my $envelope = filter2sql($node, $type); | ||
| 279 | $envelope = "ST_Transform($envelope,$type->{SRID})"; | ||
| 280 | return "($property && $envelope)"; | ||
| 281 | |||
| 282 | } elsif ($spatial2op{$name}) { | ||
| 283 | $node = $node->firstChild; | ||
| 284 | my $geom1 = filter2sql($node, $type); | ||
| 285 | $node = $node->nextSibling; | ||
| 286 | my $geom2 = filter2sql($node, $type); | ||
| 287 | return "ST_$name($geom1, $geom2)"; | ||
| 288 | |||
| 289 | } elsif ($name eq 'DWithin') { | ||
| 290 | $node = $node->firstChild; | ||
| 291 | my $geom1 = filter2sql($node, $type); | ||
| 292 | $node = $node->nextSibling; | ||
| 293 | my $geom2 = filter2sql($node, $type); | ||
| 294 | $node = $node->nextSibling; | ||
| 295 | my $dist = filter2sql($node, $type); | ||
| 296 | return "ST_$name($geom1, $geom2, $dist)"; | ||
| 297 | |||
| 298 | # temporal filter missing | ||
| 299 | |||
| 300 | # functions missing | ||
| 301 | |||
| 302 | } elsif ($name eq 'Filter') { | ||
| 303 | my %id = ( ResourceId => 1, | ||
| 304 | FeatureId => 1, | ||
| 305 | GmlObjectId => 1 ); | ||
| 306 | $node = $node->firstChild; | ||
| 307 | my ($ns2, $name2) = parse_tag($node); | ||
| 308 | if ($id{$name2}) { | ||
| 309 | my $where = ''; | ||
| 310 | for (my $id = $node; $id; $id = $id->nextSibling) { | ||
| 311 | $where .= filter2sql($id, $type).' OR '; | ||
| 312 | } | ||
| 313 | $where =~ s/ OR $//; | ||
| 314 | return $where; | ||
| 315 | } else { | ||
| 316 | return filter2sql($node, $type); | ||
| 317 | } | ||
| 318 | } | ||
| 319 | } | ||
| 320 | |||
| 321 | sub node2sql { | ||
| 322 | my ($node) = @_; | ||
| 323 | my $srs = get_integer($node->getAttribute('srsName')) // '4326'; | ||
| 324 | $srs = ",$srs" if $srs ne ''; | ||
| 325 | my ($ns, $name) = parse_tag($node); | ||
| 326 | my $wkt; | ||
| 327 | |||
| 328 | # should use Geo::OGC::Geometry | ||
| 329 | |||
| 330 | if ($name eq 'Box') { | ||
| 331 | my $env = strip($node->firstChild->textContent); | ||
| 332 | $env =~ s/ /,/; | ||
| 333 | return "ST_MakeEnvelope($env$srs)"; | ||
| 334 | } elsif ($name eq 'Envelope') { | ||
| 335 | $node = $node->firstChild; | ||
| 336 | my $lc = strip($node->textContent); # gml:lowerCorner | ||
| 337 | $lc =~ s/ /,/; | ||
| 338 | $node = $node->nextSibling; | ||
| 339 | my $uc = strip($node->textContent); # gml:upperCorner | ||
| 340 | $uc =~ s/ /,/; | ||
| 341 | return "ST_MakeEnvelope($lc,$uc$srs)"; | ||
| 342 | } elsif ($name eq 'Point') { | ||
| 343 | my $pos = $node->firstChild->firstChild->data; | ||
| 344 | $pos =~ s/,/ /; | ||
| 345 | $wkt = "POINT ($pos)"; | ||
| 346 | } elsif ($name eq 'MultiPoint') { # not ok | ||
| 347 | my $pos = $node->firstChild->firstChild->data; | ||
| 348 | $pos =~ s/,/ /; | ||
| 349 | $wkt = "MULTIPOINT ($pos)"; | ||
| 350 | } elsif ($name eq 'LineString') { | ||
| 351 | my @tmp = split /\s+/, $node->firstChild->firstChild->data; | ||
| 352 | my @pos; | ||
| 353 | for (my $i = 0; $i < @tmp; $i+=2) { | ||
| 354 | push @pos, $tmp[$i].' '.$tmp[$i+1]; | ||
| 355 | } | ||
| 356 | $wkt = "LINESTRING (".join(', ',@pos).")"; | ||
| 357 | } elsif ($name eq 'MultiLineString') { # not ok | ||
| 358 | my @tmp = split / /, $node->firstChild->firstChild->data; | ||
| 359 | my @pos; | ||
| 360 | for (my $i = 0; $i < @tmp; $i+=2) { | ||
| 361 | push @pos, $tmp[$i].' '.$tmp[$i+1]; | ||
| 362 | } | ||
| 363 | $wkt = "MULTILINESTRING (".join(', ',@pos).")"; | ||
| 364 | } elsif ($name eq 'Polygon') { | ||
| 365 | # Polygon.exterior.LinearRing.posList | ||
| 366 | my @tmp = split /\s+/, $node->firstChild->firstChild->firstChild->textContent; | ||
| 367 | my @pos; | ||
| 368 | for (my $i = 0; $i < @tmp; $i+=2) { | ||
| 369 | push @pos, strip($tmp[$i]).' '.strip($tmp[$i+1]); | ||
| 370 | } | ||
| 371 | $wkt = "POLYGON ((".join(', ',@pos)."))"; | ||
| 372 | } elsif ($name eq 'MultiPolygon') { # not ok | ||
| 373 | my @tmp = split / /, $node->firstChild->firstChild->firstChild->data; | ||
| 374 | my @pos; | ||
| 375 | for (my $i = 0; $i < @tmp; $i+=2) { | ||
| 376 | push @pos, $tmp[$i].' '.$tmp[$i+1]; | ||
| 377 | } | ||
| 378 | $wkt = "MULTIPOLYGON ((".join(', ',@pos)."))"; | ||
| 379 | } elsif ($name eq 'MultiGeometry') { # not ok | ||
| 380 | my @tmp = split / /, $node->firstChild->firstChild->firstChild->data; | ||
| 381 | my @pos; | ||
| 382 | for (my $i = 0; $i < @tmp; $i+=2) { | ||
| 383 | push @pos, $tmp[$i].' '.$tmp[$i+1]; | ||
| 384 | } | ||
| 385 | $wkt = "MULTIGEOMETRY ((".join(', ',@pos)."))"; | ||
| 386 | } | ||
| 387 | |||
| 388 | return "ST_GeometryFromText('$wkt'$srs)"; | ||
| 389 | } | ||
| 390 | |||
| 391 | sub latest_version { | ||
| 392 | my $versions = shift; | ||
| 393 | return undef unless defined $versions; | ||
| 394 | my @versions = split /\s*,\s*/, $versions; | ||
| 395 | for (@versions) { | ||
| 396 | my ($a,$b,$c) = split /\./; | ||
| 397 | $_ = $a*10000+$b*100+$c; | ||
| 398 | } | ||
| 399 | @versions = sort {$b <=> $a} @versions; | ||
| 400 | my ($a,$b,$c) = $versions[0] =~ /^(\d+)(\d\d)(\d\d)$/; | ||
| 401 | for ($a,$b,$c) { | ||
| 402 | $_ = int($_); | ||
| 403 | } | ||
| 404 | return "$a.$b.$c"; | ||
| 405 | } | ||
| 406 | |||
| 407 | sub get_integer { | ||
| 408 | my $s = shift; | ||
| 409 | if (not defined $s) { | ||
| 410 | if (@_) { | ||
| 411 | return get_integer(shift); | ||
| 412 | } else { | ||
| 413 | return undef; | ||
| 414 | } | ||
| 415 | } | ||
| 416 | if ($s =~ /(\d+)/) { | ||
| 417 | return $1; | ||
| 418 | } | ||
| 419 | return undef; | ||
| 420 | } | ||
| 421 | |||
| 422 | sub parse_tag { | ||
| 423 | my $node = shift; | ||
| 424 | return parse_name($node->nodeName); | ||
| 425 | } | ||
| 426 | |||
| 427 | sub parse_name { | ||
| 428 | my $name = shift; | ||
| 429 | my $ns = ''; | ||
| 430 | ($ns, $name) = split /:/, $name if $name =~ /:/; | ||
| 431 | return ($ns, $name); | ||
| 432 | } | ||
| 433 | |||
| 434 | sub strip { | ||
| 435 | my $s = shift; | ||
| 436 | $s =~ s/^\s+//; | ||
| 437 | $s =~ s/\s+$//; | ||
| 438 | return $s; | ||
| 439 | } | ||
| 440 | |||
| 441 | 1; |