| 1 | # Copyrights 2008-2018 by [Mark Overmeer]. | ||
| 2 | # For other contributors see ChangeLog. | ||
| 3 | # See the manual pages for details on the licensing terms. | ||
| 4 | # Pod stripped from pm file by OODoc 2.02. | ||
| 5 | # This code is part of distribution Geo-WKT. Meta-POD processed with | ||
| 6 | # OODoc into POD and HTML manual-pages. See README.md | ||
| 7 | # Copyright Mark Overmeer. Licensed under the same terms as Perl itself. | ||
| 8 | |||
| 9 | package Geo::WKT; | ||
| 10 | 7 | ▶ | use vars '$VERSION'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | $VERSION = '0.96'; | ||
| 12 | |||
| 13 | 7 | ▶ | use base 'Exporter'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | |||
| 15 | 7 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | 7 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 17 | |||
| 18 | 0 | ▶ | use Geo::Shape (); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 19 | use Carp; | ||
| 20 | |||
| 21 | our @EXPORT = qw( | ||
| 22 | parse_wkt | ||
| 23 | parse_wkt_point | ||
| 24 | parse_wkt_polygon | ||
| 25 | parse_wkt_geomcol | ||
| 26 | parse_wkt_linestring | ||
| 27 | wkt_point | ||
| 28 | wkt_multipoint | ||
| 29 | wkt_linestring | ||
| 30 | wkt_polygon | ||
| 31 | wkt_linestring | ||
| 32 | wkt_multilinestring | ||
| 33 | wkt_multipolygon | ||
| 34 | wkt_optimal | ||
| 35 | wkt_geomcollection | ||
| 36 | ); | ||
| 37 | |||
| 38 | sub wkt_optimal($); | ||
| 39 | |||
| 40 | |||
| 41 | sub parse_wkt_point($;$) | ||
| 42 | { ($_[0] =~ m/^point\(\s*(\S+)\s+(\S+)\)$/i) | ||
| 43 | ? Geo::Point->xy($1+0, $2+0, $_[1]) | ||
| 44 | : undef; | ||
| 45 | } | ||
| 46 | |||
| 47 | |||
| 48 | sub parse_wkt_polygon($;$) | ||
| 49 | { my ($string, $proj) = @_; | ||
| 50 | |||
| 51 | $string && $string =~ m/^polygon\(\((.+)\)\)$/i | ||
| 52 | or return undef; | ||
| 53 | |||
| 54 | my @poly; | ||
| 55 | foreach my $poly (split m/\)\s*\,\s*\(/, $1) | ||
| 56 | { my @points = map +[split " ", $_, 2], split /\s*\,\s*/, $poly; | ||
| 57 | push @poly, \@points; | ||
| 58 | } | ||
| 59 | |||
| 60 | Geo::Surface->new(@poly, proj => $proj); | ||
| 61 | } | ||
| 62 | |||
| 63 | |||
| 64 | sub parse_wkt_geomcol($;$) | ||
| 65 | { my ($string, $proj) = @_; | ||
| 66 | |||
| 67 | return undef if $string !~ | ||
| 68 | s/^(multiline|multipoint|multipolygon|geometrycollection)\(//i; | ||
| 69 | |||
| 70 | my @comp; | ||
| 71 | while($string =~ m/\D/) | ||
| 72 | { $string =~ s/^([^(]*\([^)]*\))// | ||
| 73 | or last; | ||
| 74 | |||
| 75 | my $take = $1; | ||
| 76 | while(1) | ||
| 77 | { my @open = $take =~ m/\(/g; | ||
| 78 | my @close = $take =~ m/\)/g; | ||
| 79 | last if @open==@close; | ||
| 80 | $take .= $1 if $string =~ s/^([^\)]*\))//; | ||
| 81 | } | ||
| 82 | push @comp, parse_wkt($take, $proj); | ||
| 83 | $string =~ s/^\s*\,\s*//; | ||
| 84 | } | ||
| 85 | |||
| 86 | Geo::Space->new(@comp, proj => $proj); | ||
| 87 | } | ||
| 88 | |||
| 89 | |||
| 90 | sub parse_wkt_linestring($;$) | ||
| 91 | { my ($string, $proj) = @_; | ||
| 92 | |||
| 93 | $string && $string =~ m/^linestring\((.+)\)$/i | ||
| 94 | or return undef; | ||
| 95 | |||
| 96 | my @points = map +[split " ", $_, 2], split /\s*\,\s*/, $1; | ||
| 97 | @points > 1 or return; | ||
| 98 | |||
| 99 | Geo::Line->new(proj => $proj, points => \@points, filled => 0); | ||
| 100 | } | ||
| 101 | |||
| 102 | |||
| 103 | sub parse_wkt($;$) # dirty code to avoid copying the sometimes huge string | ||
| 104 | { | ||
| 105 | $_[0] =~ m/^point\(/i ? &parse_wkt_point | ||
| 106 | : $_[0] =~ m/^polygon\(/i ? &parse_wkt_polygon | ||
| 107 | : $_[0] =~ m/^linestring\(/i ? &parse_wkt_linestring | ||
| 108 | : &parse_wkt_geomcol; | ||
| 109 | } | ||
| 110 | |||
| 111 | |||
| 112 | sub _list_of_points(@) | ||
| 113 | { my @points | ||
| 114 | = @_ > 1 ? @_ | ||
| 115 | : ref $_[0] eq 'ARRAY' ? @{$_[0]} | ||
| 116 | : $_[0]->isa('Math::Polygon') ? $_[0]->points | ||
| 117 | : $_[0]; | ||
| 118 | |||
| 119 | my @s = map | ||
| 120 | { (ref $_ ne 'ARRAY' && $_->isa('Geo::Point')) | ||
| 121 | ? $_->x.' '.$_->y | ||
| 122 | : $_->[0].' '.$_->[1] | ||
| 123 | } @points; | ||
| 124 | |||
| 125 | local $" = ','; | ||
| 126 | "(@s)"; | ||
| 127 | } | ||
| 128 | |||
| 129 | sub wkt_point($;$) | ||
| 130 | { my ($x, $y) | ||
| 131 | = @_==2 ? @_ | ||
| 132 | : ref $_[0] eq 'ARRAY' ? @{$_[0]} | ||
| 133 | : shift->xy; | ||
| 134 | |||
| 135 | defined $x && defined $y ? "POINT($x $y)" : (); | ||
| 136 | } | ||
| 137 | |||
| 138 | |||
| 139 | sub wkt_linestring(@) { 'LINESTRING' . _list_of_points(@_) } | ||
| 140 | |||
| 141 | |||
| 142 | sub wkt_polygon(@) | ||
| 143 | { my @polys | ||
| 144 | = !defined $_[0] ? return () | ||
| 145 | : ref $_[0] eq 'ARRAY' ? (ref $_[0][0] ? @_ : [@_]) | ||
| 146 | : $_[0]->isa('Geo::Line') ? @_ | ||
| 147 | : $_[0]->isa('Geo::Surface') ? ($_[0]->outer, $_[0]->inner) | ||
| 148 | : [@_]; | ||
| 149 | |||
| 150 | 'POLYGON(' .join(',' , map _list_of_points($_), @polys). ')'; | ||
| 151 | } | ||
| 152 | |||
| 153 | |||
| 154 | sub wkt_multipoint(@) { 'MULTIPOINT(' .join(',', map wkt_point($_), @_). ')'} | ||
| 155 | |||
| 156 | |||
| 157 | sub wkt_multilinestring(@) | ||
| 158 | { return () unless @_; | ||
| 159 | 'MULTILINESTRING(' .join(',' , map wkt_linestring($_), @_). ')'; | ||
| 160 | } | ||
| 161 | |||
| 162 | |||
| 163 | sub wkt_multipolygon(@) | ||
| 164 | { return () unless @_; | ||
| 165 | |||
| 166 | my @polys = map wkt_polygon($_), @_; | ||
| 167 | s/^POLYGON// for @polys; | ||
| 168 | |||
| 169 | 'MULTIPOLYGON(' .join(',' , @polys). ')'; | ||
| 170 | } | ||
| 171 | |||
| 172 | |||
| 173 | |||
| 174 | sub wkt_optimal($) | ||
| 175 | { my $geom = shift; | ||
| 176 | return wkt_point(undef) unless defined $geom; | ||
| 177 | |||
| 178 | return wkt_point($geom) | ||
| 179 | if $geom->isa('Geo::Point'); | ||
| 180 | |||
| 181 | return ( $geom->isRing && $geom->isFilled | ||
| 182 | ? wkt_polygon($geom) | ||
| 183 | : wkt_linestring($geom)) | ||
| 184 | if $geom->isa('Geo::Line'); | ||
| 185 | |||
| 186 | return wkt_multipolygon($geom) | ||
| 187 | if $geom->isa('Geo::Surface'); | ||
| 188 | |||
| 189 | $geom->isa('Geo::Space') | ||
| 190 | or croak "ERROR: Cannot translate object $geom into SQL"; | ||
| 191 | |||
| 192 | $geom->nrComponents==1 ? wkt_optimal($geom->component(0)) | ||
| 193 | : $geom->onlyPoints ? wkt_multipoint($geom->points) | ||
| 194 | : wkt_geomcollection($geom); | ||
| 195 | } | ||
| 196 | |||
| 197 | |||
| 198 | sub wkt_geomcollection(@) | ||
| 199 | { @_ = $_[0]->components | ||
| 200 | if @_==1 | ||
| 201 | && ref $_[0] ne 'ARRAY' | ||
| 202 | && $_[0]->isa('Geo::Space'); | ||
| 203 | |||
| 204 | 'GEOMETRYCOLLECTION(' .join(',', map wkt_optimal($_), @_). ')'; | ||
| 205 | } | ||
| 206 | |||
| 207 | 1; | ||