| 1 | package DBIO::PostgreSQL::PostGIS::ResultSet; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Spatial query helpers for PostGIS-aware result classes | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 7 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 7 | ▶ | use base 'DBIO::ResultSet'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 7 | ▶ | use DBIO::Exception (); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 7 | ▶ | use DBIO::PostgreSQL::Util (); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | # Render the user-supplied geometry argument into ('SQL fragment', @binds) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | # suitable for splicing into a -bool/-and ScalarRef literal. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 42 | ▶ | sub _geom_arg { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_geom_arg: called CC=11 SCAR=25.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 42 | my ($self, $geom) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 42 | ▶ | if (ref($geom) eq 'SCALAR') { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 0 | return ($$geom); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 42 | ▶ | if (ref($geom) eq 'REF' && ref($$geom) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $geom eq "REF" and ref $$geom eq "ARRAY"
MC/DC0%0/2
ref $geom eq "REF" and ref $$geom eq "ARRAY"
ref $geom eq "REF"
ref $$geom eq "ARRAY"
Truth table33%1/3
A and B
A= ref $geom eq "REF"B= ref $$geom eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 0 | my ($sql, @binds) = @{$$geom}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | return ($sql, @binds); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 42 | ▶ | if (ref($geom) && $geom->isa('DBIO::PostgreSQL::PostGIS::Geometry')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $geom and $geom->isa("DBIO::PostgreSQL::PostGIS::Geometry")
MC/DC50%1/2
ref $geom and $geom->isa("DBIO::PostgreSQL::PostGIS::Geometry")
ref $geom
$geom->isa("DBIO::PostgreSQL::PostGIS::Geometry")
Truth table66%2/3
A and B
A= ref $geomB= $geom->isa("DBIO::PostgreSQL::PostGIS::Geometry")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 8 | my $srid = $geom->srid; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | # A value read from the database goes back as the EWKB hex it arrived | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | # as: its WKT is a rendering, 15 digits per ordinate, and not the | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | # stored geometry (karr k22). The hex carries its own SRID flag; the | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | # object's srid, when it has one, is set over it as before. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 8 | my $hex = $geom->ewkb_hex; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 8 | ▶ | if (defined $hex && length $hex) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $hex and length $hex
MC/DC50%1/2
defined $hex and length $hex
defined $hex
length $hex
Truth table66%2/3
A and B
A= defined $hexB= length $hex
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 2 | ▶ | return ('ST_SetSRID(?::geometry, ?)', $hex, $srid) if defined $srid; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 1 | return ('?::geometry', $hex); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 6 | my $why = $geom->_unwritable_reason; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 6 | ▶ | DBIO::Exception->throw($why) if defined $why; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 2 | my $wkt = $geom->wkt; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 2 | ▶ | if (defined $srid) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 1 | return ('ST_GeomFromText(?, ?)', $wkt, $srid); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 1 | return ('ST_GeomFromText(?)', $wkt); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | # Plain EWKT string | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 34 | return ('ST_GeomFromEWKT(?)', $geom); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | # The quoted reference to a column of the source: "alias"."column". The name | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | # is an identifier of the source, never SQL. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 33 | ▶ | sub _col_ref { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_col_ref: called CC=5 SCAR=17.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 33 | my ($self, $col) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 33 | ▶ | DBIO::Exception->throw("no column '" . ($col // '') . "' in the source of this resultset") | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%4/8
defined $col and not ref $col and $self->result_source->has_column($col)
defined $col and not ref $col
$col // ""
MC/DC33%1/3
defined $col and not ref $col and $self->result_source->has_column($col)
defined $col
not ref $col
$self->result_source->has_column($col)
MC/DC0%0/1
$col // ""
$col
Truth table50%2/4
A and not(B) and C
A= defined $colB= not ref $colC= $self->result_source->has_column($col)
Truth table50%1/2
A
A= $col
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | unless defined $col && !ref $col && $self->result_source->has_column($col); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 22 | return DBIO::PostgreSQL::Util::quote_ident($self->current_source_alias) | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | . '.' . DBIO::PostgreSQL::Util::quote_ident($col); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 23 | ▶ | sub _spatial_search { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_spatial_search: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 23 | my ($self, $sql_func, $col, $geom, @extra) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 23 | my ($geom_sql, @binds) = $self->_geom_arg($geom); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 21 | my $ref = $self->_col_ref($col); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 14 | ▶ | return $self->search({ | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | -bool => \[ "$sql_func($ref, $geom_sql" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | . (@extra ? ', ' . join(', ', ('?') x scalar @extra) : '') | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | . ')', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | @binds, @extra ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 7 | my @_SIMPLE_PREDICATES = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | [ intersects => 'ST_Intersects' ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | [ contains => 'ST_Contains' ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | [ within => 'ST_Within' ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | [ touches => 'ST_Touches' ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | [ crosses => 'ST_Crosses' ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | [ overlaps => 'ST_Overlaps' ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 7 | for my $pred (@_SIMPLE_PREDICATES) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 42 | my ($name, $func) = @$pred; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 7 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 42 | ▶ | *{__PACKAGE__ . "::$name"} = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 18 | my ($self, $col, $geom) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 18 | return $self->_spatial_search($func, $col, $geom); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 42 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 3 | ▶ | sub within_distance { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
within_distance: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 3 | my ($self, $col, $geom, $dist) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 3 | return $self->_spatial_search('ST_DWithin', $col, $geom, $dist); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 3 | ▶ | sub bbox_intersects { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
bbox_intersects: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 3 | my ($self, $col, $geom) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 3 | my ($geom_sql, @binds) = $self->_geom_arg($geom); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 3 | my $ref = $self->_col_ref($col); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 2 | return $self->search({ | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | -bool => \[ "$ref && $geom_sql", @binds ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 3 | ▶ | sub nearest_to { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
nearest_to: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 3 | my ($self, $col, $geom, $limit) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 3 | my ($geom_sql, @binds) = $self->_geom_arg($geom); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 3 | my $ref = $self->_col_ref($col); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 2 | ▶ | my $rs = $self->search(undef, { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | order_by => \[ "$ref <-> $geom_sql", @binds ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | ($limit ? (rows => $limit) : ()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 2 | return $rs; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 3 | ▶ | sub order_by_distance { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
order_by_distance: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 3 | my ($self, $col, $geom) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 3 | my ($geom_sql, @binds) = $self->_geom_arg($geom); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 3 | my $ref = $self->_col_ref($col); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 2 | return $self->search(undef, { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | order_by => \[ "ST_Distance($ref, $geom_sql)", @binds ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 3 | ▶ | sub with_distance { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
with_distance: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 3 | my ($self, $col, $geom, $alias) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 3 | ▶ | $alias //= 'distance'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$alias //= "distance"
MC/DC0%0/1
$alias //= "distance"
$alias
Truth table50%1/2
A
A= $alias
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 3 | my ($geom_sql, @binds) = $self->_geom_arg($geom); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 3 | my $ref = $self->_col_ref($col); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 2 | return $self->search(undef, { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | '+select' => [ \[ "ST_Distance($ref, $geom_sql)", @binds ] ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | '+as' => [ $alias ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 7 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||