98
99
%rename (_SetGeometryDirectly) SetGeometryDirectly;
99
100
%rename (_ExportToWkb) ExportToWkb;
100
101
%rename (_GetDriver) GetDriver;
102
%rename (_TestCapability) TestCapability;
106
package Geo::OGR::Driver;
111
package Geo::OGR::Driver;
108
use vars qw /@CAPABILITIES/;
109
for my $s (qw/CreateDataSource DeleteDataSource/) {
113
use vars qw /@CAPABILITIES %CAPABILITIES/;
114
@CAPABILITIES = qw/CreateDataSource DeleteDataSource/;
115
for my $s (@CAPABILITIES) {
110
116
my $cap = eval "\$Geo::OGR::ODrC$s";
111
push @CAPABILITIES, $cap;
117
$CAPABILITIES{$s} = $cap;
113
119
sub Capabilities {
114
120
return @CAPABILITIES if @_ == 0;
115
121
my $self = shift;
117
123
for my $cap (@CAPABILITIES) {
118
push @cap, $cap if TestCapability($self, $cap);
124
push @cap, $cap if _TestCapability($self, $CAPABILITIES{$cap});
129
my($self, $cap) = @_;
130
return _TestCapability($self, $CAPABILITIES{$cap});
122
132
*Create = *CreateDataSource;
123
133
*Copy = *CopyDataSource;
124
134
*OpenDataSource = *Open;
127
137
package Geo::OGR::DataSource;
130
use vars qw /@CAPABILITIES %LAYERS/;
131
for my $s (qw/CreateLayer DeleteLayer/) {
140
use vars qw /@CAPABILITIES %CAPABILITIES %LAYERS/;
141
@CAPABILITIES = qw/CreateLayer DeleteLayer/;
142
for my $s (@CAPABILITIES) {
132
143
my $cap = eval "\$Geo::OGR::ODsC$s";
133
push @CAPABILITIES, $cap;
144
$CAPABILITIES{$s} = $cap;
135
146
sub Capabilities {
136
147
return @CAPABILITIES if @_ == 0;
137
148
my $self = shift;
139
150
for my $cap (@CAPABILITIES) {
140
push @cap, $cap if TestCapability($self, $cap);
151
push @cap, $cap if _TestCapability($self, $CAPABILITIES{$cap});
156
my($self, $cap) = @_;
157
return _TestCapability($self, $CAPABILITIES{$cap});
144
159
*GetDriver = *_GetDriver;
158
173
if (defined $name) {
159
174
$layer = _GetLayerByName($self, "$name");
175
$layer = _GetLayerByIndex($self, $name) unless $layer;
177
$layer = _GetLayerByIndex($self, 0);
163
$layer = _GetLayerByIndex($self, $name) if !$layer and $name =~ /^\d+$/;
164
return unless $layer;
179
croak "No such layer: $name\n" unless $layer;
165
180
$LAYERS{tied(%$layer)} = $self;
178
193
my($self, $index) = @_;
179
194
$index = 0 unless defined $index;
180
195
my $layer = _GetLayerByIndex($self, $index+0);
181
return unless $layer;
196
croak "No such layer: $index\n" unless $layer;
182
197
$LAYERS{tied(%$layer)} = $self;
185
200
sub GetLayerByName {
186
201
my($self, $name) = @_;
187
my $layer = _GetLayerByName($self, $name);
188
return unless $layer;
202
my $layer = _GetLayerByName($self, "$name");
203
croak "No such layer: $name\n" unless $layer;
189
204
$LAYERS{tied(%$layer)} = $self;
237
252
package Geo::OGR::Layer;
239
use vars qw /@CAPABILITIES/;
240
for my $s (qw/RandomRead SequentialWrite RandomWrite
254
use vars qw /@CAPABILITIES %CAPABILITIES/;
255
@CAPABILITIES = qw/RandomRead SequentialWrite RandomWrite
241
256
FastSpatialFilter FastFeatureCount FastGetExtent
242
CreateField Transactions DeleteFeature FastSetNextByIndex/) {
257
CreateField DeleteField ReorderFields AlterFieldDefn
258
Transactions DeleteFeature FastSetNextByIndex
259
StringsAsUTF8 IgnoreFields/;
260
for my $s (@CAPABILITIES) {
243
261
my $cap = eval "\$Geo::OGR::OLC$s";
244
push @CAPABILITIES, $cap;
262
$CAPABILITIES{$s} = $cap;
267
285
my $self = shift;
269
287
for my $cap (@CAPABILITIES) {
270
push @cap, $cap if TestCapability($self, $cap);
288
push @cap, $cap if _TestCapability($self, $CAPABILITIES{$cap});
293
my($self, $cap) = @_;
294
return _TestCapability($self, $CAPABILITIES{$cap});
275
297
my $self = shift;
280
302
if (ref($fd) eq 'HASH') {
281
303
$fd = Geo::OGR::FieldDefn->create(%$fd);
305
$schema{ApproxOK} = 1 unless defined $schema{ApproxOK};
283
306
CreateField($self, $fd, $schema{ApproxOK});
305
328
for my $fn (keys %row) {
306
329
next if $fn eq 'FID';
307
330
next if $fn eq 'Geometry';
308
if (defined $row{$fn}) {
309
$f->SetField($fn, $row{$fn});
331
$f->SetField($fn, $row{$fn});
315
334
$self->SetFeature($f) if $changed;
389
408
$self->CreateFeature($f);
412
return $Geo::OGR::Geometry::TYPE_INT2STRING{GetGeomType($self)};
392
415
package Geo::OGR::FeatureDefn;
426
my $self = Geo::OGRc::new_FeatureDefn($schema{Name});
428
$self->GeometryType($schema{GeometryType});
429
for my $fd (@{$schema{Fields}}) {
430
if (ref($fd) eq 'HASH') {
431
$fd = Geo::OGR::FieldDefn->create(%$fd);
433
AddFieldDefn($self, $fd);
395
439
my $self = shift;
410
454
$schema{GeometryType} = $self->GeomType();
411
455
$schema{Fields} = [];
412
456
for my $i (0..$self->GetFieldCount-1) {
413
push @{$schema{Fields}}, $self->GetFieldDefn($i)->Schema;
457
my $s = $self->GetFieldDefn($i)->Schema;
459
push @{$schema{Fields}}, $s;
461
return wantarray ? %schema : \%schema;
418
464
my($self, $type) = @_;
424
470
return $Geo::OGR::Geometry::TYPE_INT2STRING{GetGeomType($self)} if defined wantarray;
426
472
*GeometryType = *GeomType;
473
sub GeometryIgnored {
475
SetGeometryIgnored($self, $_[0]) if @_;
476
IsGeometryIgnored($self) if defined wantarray;
480
SetStyleIgnored($self, $_[0]) if @_;
481
IsStyleIgnored($self) if defined wantarray;
428
484
package Geo::OGR::Feature;
430
486
use vars qw /%GEOMETRIES/;
491
$pkg->new(Geo::OGR::FeatureDefn->create(@_));
494
my($self, $index) = @_;
495
$self->GetField($index);
433
502
my $self = shift;
434
503
$self->SetFID($_[0]) if @_;
450
519
if (ref($fd) eq 'HASH') {
451
520
$fd = Geo::OGR::FieldDefn->create(%$fd);
522
$schema{ApproxOK} = 1 unless defined $schema{ApproxOK};
453
523
CreateField($self, $fd, $schema{ApproxOK});
472
542
for my $fn (keys %row) {
473
543
next if $fn eq 'FID';
474
544
next if $fn eq 'Geometry';
475
if (defined $row{$fn}) {
476
$self->SetField($fn, $row{$fn});
478
$self->UnsetField($fn);
545
$self->SetField($fn, $row{$fn});
481
547
return unless defined wantarray;
530
596
sub GetFieldType {
531
597
my($self, $field) = @_;
598
my $index = GetFieldIndex($self, "$field");
599
$field = $index unless $index == -1;
600
croak "No such field: $field" if $field < 0 or $field >= GetFieldCount($self);
532
601
return $Geo::OGR::FieldDefn::TYPE_INT2STRING{_GetFieldType($self, $field)};
534
603
sub FieldIsList {
535
604
my($self, $field) = @_;
536
my $count = GetFieldCount($self);
537
$field = GetFieldIndex($self, $field) unless $field =~ /^\d+$/;
538
croak("no such field: $_[1]") if $field < 0 or $field >= $count;
605
my $index = GetFieldIndex($self, "$field");
606
$field = $index unless $index == -1;
607
croak "No such field: $field" if $field < 0 or $field >= GetFieldCount($self);
539
608
my $type = _GetFieldType($self, $field);
540
609
return 1 if ($type == $Geo::OGR::OFTIntegerList or
541
610
$type == $Geo::OGR::OFTRealList or
549
618
my($self, $field) = @_;
550
my $count = GetFieldCount($self);
551
$field = GetFieldIndex($self, $field) unless $field =~ /^\d+$/;
552
croak("no such field: $_[1]") if $field < 0 or $field >= $count;
619
my $index = GetFieldIndex($self, "$field");
620
$field = $index unless $index == -1;
621
croak "No such field: $field" if $field < 0 or $field >= GetFieldCount($self);
553
622
return undef unless IsFieldSet($self, $field);
554
623
my $type = _GetFieldType($self, $field);
555
624
if ($type == $Geo::OGR::OFTInteger) {
564
633
if ($type == $Geo::OGR::OFTIntegerList) {
565
634
my $ret = GetFieldAsIntegerList($self, $field);
635
return wantarray ? @$ret : $ret;
568
637
if ($type == $Geo::OGR::OFTRealList) {
569
638
my $ret = GetFieldAsDoubleList($self, $field);
639
return wantarray ? @$ret : $ret;
572
641
if ($type == $Geo::OGR::OFTStringList) {
573
642
my $ret = GetFieldAsStringList($self, $field);
643
return wantarray ? @$ret : $ret;
576
645
if ($type == $Geo::OGR::OFTBinary) {
577
646
return GetFieldAsString($self, $field);
579
648
if ($type == $Geo::OGR::OFTDate) {
580
649
my @ret = GetFieldAsDateTime($self, $field);
581
650
# year, month, day, hour, minute, second, timezone
651
return wantarray ? @ret[0..2] : [@ret[0..2]];
584
653
if ($type == $Geo::OGR::OFTTime) {
585
654
my @ret = GetFieldAsDateTime($self, $field);
655
return wantarray ? @ret[3..6] : [@ret[3..6]];
588
657
if ($type == $Geo::OGR::OFTDateTime) {
589
658
return GetFieldAsDateTime($self, $field);
594
663
my($self, $field) = @_;
595
my $type = _GetFieldType($self, $field);
596
my $count = GetFieldCount($self);
597
$field = GetFieldIndex($self, $field) unless $field =~ /^\d+$/;
598
croak("no such field: $_[1]") if $field < 0 or $field >= $count;
664
my $index = GetFieldIndex($self, "$field");
665
$field = $index unless $index == -1;
666
croak "No such field: $field" if $field < 0 or $field >= GetFieldCount($self);
599
667
_UnsetField($self, $field);
602
670
my $self = shift;
603
671
my $field = $_[0];
604
my $count = GetFieldCount($self);
605
$field = GetFieldIndex($self, $field) unless $field =~ /^\d+$/;
606
croak("no such field: $_[0]") if $field < 0 or $field >= $count;
672
my $index = GetFieldIndex($self, "$field");
673
$field = $index unless $index == -1;
674
croak "No such field: $field" if $field < 0 or $field >= GetFieldCount($self);
608
676
if (@_ == 0 or !defined($_[0])) {
609
677
_UnsetField($self, $field);
612
680
my $list = ref($_[0]) ? $_[0] : [@_];
613
681
my $type = _GetFieldType($self, $field);
614
if ($type == $Geo::OGR::OFTInteger or
615
$type == $Geo::OGR::OFTReal or
682
if ($type == $Geo::OGR::OFTInteger or
683
$type == $Geo::OGR::OFTReal or
616
684
$type == $Geo::OGR::OFTString or
617
$type == $Geo::OGR::OFTBinary)
685
$type == $Geo::OGR::OFTBinary)
619
687
_SetField($self, $field, $_[0]);
668
736
$GEOMETRIES{tied(%$geom)} = $self if $geom;
739
sub ReferenceGeometry {
741
SetGeometryDirectly($self, $_[0]) if @_;
742
if (defined wantarray) {
743
my $geometry = GetGeometry($self);
744
return $geometry->Clone() if $geometry;
748
my($self, $other) = @_;
749
_SetFrom($self, $other), return if @_ <= 2;
750
my $forgiving = $_[2];
751
_SetFrom($self, $other, $forgiving), return if @_ <= 3;
754
for my $i (1..GetFieldCount($self)) {
755
push @list, ($map->{$i} || -1);
757
SetFromWithMap($self, $other, 1, \@list);
672
760
package Geo::OGR::FieldDefn;
763
@FIELD_TYPES @JUSTIFY_TYPES
675
764
%TYPE_STRING2INT %TYPE_INT2STRING
676
765
%JUSTIFY_STRING2INT %JUSTIFY_INT2STRING
678
for my $string (qw/Integer IntegerList Real RealList String StringList
679
WideString WideStringList Binary Date Time DateTime/) {
768
@FIELD_TYPES = qw/Integer IntegerList Real RealList String StringList
769
WideString WideStringList Binary Date Time DateTime/;
770
@JUSTIFY_TYPES = qw/Undefined Left Right/;
771
for my $string (@FIELD_TYPES) {
680
772
my $int = eval "\$Geo::OGR::OFT$string";
681
773
$TYPE_STRING2INT{$string} = $int;
682
774
$TYPE_INT2STRING{$int} = $string;
684
for my $string (qw/Undefined Left Right/) {
776
for my $string (@JUSTIFY_TYPES) {
685
777
my $int = eval "\$Geo::OGR::OJ$string";
686
778
$JUSTIFY_STRING2INT{$string} = $int;
687
779
$JUSTIFY_INT2STRING{$int} = $string;
707
$param{Type} = $TYPE_STRING2INT{$param{Type}} if defined $param{Type} and exists $TYPE_STRING2INT{$param{Type}};
799
$param{Type} = $TYPE_STRING2INT{$param{Type}}
800
if defined $param{Type} and exists $TYPE_STRING2INT{$param{Type}};
801
$param{Justify} = $JUSTIFY_STRING2INT{$param{Justify}}
802
if defined $param{Justify} and exists $JUSTIFY_STRING2INT{$param{Justify}};
708
803
my $self = Geo::OGRc::new_FieldDefn($param{Name}, $param{Type});
709
804
if (defined($self)) {
710
805
bless $self, $pkg;
745
840
SetPrecision($self, $_[0]) if @_;
746
841
GetPrecision($self) if defined wantarray;
845
SetIgnored($self, $_[0]) if @_;
846
IsIgnored($self) if defined wantarray;
749
849
my $self = shift;
756
856
$self->Precision($param{Precision}) if exists $param{Precision};
758
858
return unless defined wantarray;
759
return { Name => $self->Name,
761
Justify => $self->Justify,
762
Width => $self->Width,
763
Precision => $self->Precision };
859
my %schema = ( Name => $self->Name,
861
Justify => $self->Justify,
862
Width => $self->Width,
863
Precision => $self->Precision );
864
return wantarray ? %schema : \%schema;
766
867
package Geo::OGR::Geometry;
871
@GEOMETRY_TYPES @BYTE_ORDER_TYPES
770
872
%TYPE_STRING2INT %TYPE_INT2STRING
771
873
%BYTE_ORDER_STRING2INT %BYTE_ORDER_INT2STRING
773
for my $string (qw/Unknown
875
@GEOMETRY_TYPES = qw/Unknown
774
876
Point LineString Polygon
775
877
MultiPoint MultiLineString MultiPolygon GeometryCollection
777
879
Point25D LineString25D Polygon25D
778
MultiPoint25D MultiLineString25D MultiPolygon25D GeometryCollection25D/) {
880
MultiPoint25D MultiLineString25D MultiPolygon25D GeometryCollection25D/;
881
for my $string (@GEOMETRY_TYPES) {
779
882
my $int = eval "\$Geo::OGR::wkb$string";
780
883
$TYPE_STRING2INT{$string} = $int;
781
884
$TYPE_INT2STRING{$int} = $string;
783
for my $string (qw/XDR NDR/) {
886
@BYTE_ORDER_TYPES = qw/XDR NDR/;
887
for my $string (@BYTE_ORDER_TYPES) {
784
888
my $int = eval "\$Geo::OGR::wkb$string";
785
889
$BYTE_ORDER_STRING2INT{$string} = $int;
786
890
$BYTE_ORDER_INT2STRING{$int} = $string;
789
893
my $self = shift;
790
894
delete $Geo::OGR::Feature::GEOMETRIES{$self};
792
sub create { # alternative constructor since swig created new can't be overridden(?)
896
sub create { # alternative constructor since swig created new cannot be overridden(?)
794
my($type, $wkt, $wkb, $gml, $json, $srs, $points);
898
my($type, $wkt, $wkb, $gml, $json, $srs, $points, $arc);
800
904
$srs = ($param{srs} or $param{SRS});
801
905
$wkt = ($param{wkt} or $param{WKT});
802
906
$wkb = ($param{wkb} or $param{WKB});
803
my $hex = ($param{hexwkb} or $param{HEXWKB});
907
my $hex = ($param{hexewkb} or $param{HEXEWKB}); # PostGIS HEX EWKB
908
substr($hex, 10, 8) = '' if $hex; # remove SRID
909
$hex = ($param{hexwkb} or $param{HEXWKB}) unless $hex;
806
912
for (my $i = 0; $i < length($hex); $i+=2) {
810
916
$gml = ($param{gml} or $param{GML});
811
917
$json = ($param{geojson} or $param{GeoJSON});
812
918
$points = $param{Points};
919
$arc = ($param{arc} or $param{Arc});
814
921
$type = $TYPE_STRING2INT{$type} if defined $type and exists $TYPE_STRING2INT{$type};
817
croak "unknown GeometryType: $type" unless
818
exists($TYPE_STRING2INT{$type}) or exists($TYPE_INT2STRING{$type});
819
$self = Geo::OGRc::new_Geometry($type);
820
} elsif (defined $wkt) {
821
924
$self = Geo::OGRc::CreateGeometryFromWkt($wkt, $srs);
822
925
} elsif (defined $wkb) {
823
926
$self = Geo::OGRc::CreateGeometryFromWkb($wkb, $srs);
825
928
$self = Geo::OGRc::CreateGeometryFromGML($gml);
826
929
} elsif (defined $json) {
827
930
$self = Geo::OGRc::CreateGeometryFromJson($json);
931
} elsif (defined $type) {
932
croak "unknown GeometryType: $type" unless
933
exists($TYPE_STRING2INT{$type}) or exists($TYPE_INT2STRING{$type});
934
$self = Geo::OGRc::new_Geometry($type);
935
} elsif (defined $arc) {
936
$self = Geo::OGRc::ApproximateArcAngles(@$arc);
829
938
croak "missing GeometryType, WKT, WKB, GML, or GeoJSON parameter in Geo::OGR::Geometry::create";
832
941
$self->Points($points) if $points;
946
my $wkb = _ExportToWkb($self, 1);
948
for (my $i = 0; $i < length($wkb); $i++) {
949
my $x = sprintf("%x", ord(substr($wkb,$i,1)));
950
$x = '0' . $x if length($x) == 1;
956
my($self, $srid) = @_;
957
my $hex = AsHEXWKB($self);
959
my $s = sprintf("%x", $srid);
962
if (length($s) > 2) {
963
$srid .= substr($s,-2,2);
964
substr($s,-2,2) = '';
965
} elsif (length($s) > 1) {
976
while (length($srid) < 8) {
979
substr($hex, 10, 0) = uc($srid);
835
982
sub GeometryType {
836
983
my $self = shift;
837
984
return $TYPE_INT2STRING{$self->GetGeometryType};
882
1029
if ($t eq 'Unknown' or $t eq 'None' or $t eq 'GeometryCollection') {
883
1030
croak("Can't set points of a geometry of type: $t");
884
1031
} elsif ($t eq 'Point') {
885
$flat ? AddPoint_2D($self, @$points[0..1]) : AddPoint_3D($self, @$points[0..2]);
1032
# support both "Point" as a list of one point and one point
1033
if (ref($points->[0])) {
1035
AddPoint_2D($self, @{$points->[0]}[0..1]) :
1036
AddPoint_3D($self, @{$points->[0]}[0..2]);
1039
AddPoint_2D($self, @$points[0..1]) :
1040
AddPoint_3D($self, @$points[0..2]);
886
1042
} elsif ($t eq 'LineString' or $t eq 'LinearRing') {
888
1044
for my $p (@$points) {
935
1091
$n = $self->GetPointCount;
937
push @points, $flat ? GetPoint_2D($self) : GetPoint_3D($self);
1093
push @points, $flat ? scalar GetPoint_2D($self) : scalar GetPoint_3D($self);
941
1097
for my $i (0..$n-1) {
942
push @points, GetPoint_2D($self, $i);
1098
push @points, scalar GetPoint_2D($self, $i);
945
1101
for my $i (0..$n-1) {
946
push @points, GetPoint_3D($self, $i);
1102
push @points, scalar GetPoint_3D($self, $i);
955
1111
$bo = $BYTE_ORDER_STRING2INT{$bo} if defined $bo and exists $BYTE_ORDER_STRING2INT{$bo};
956
1112
return _ExportToWkb($self, $bo);
1114
sub ForceToMultiPoint {
1116
$self = Geo::OGR::ForceToMultiPoint($self);
1118
$self->AddGeometry($g);
1122
sub ForceToMultiLineString {
1124
$self = Geo::OGR::ForceToMultiLineString($self);
1126
$self->AddGeometry($g);
1130
sub ForceToMultiPolygon {
1132
$self = Geo::OGR::ForceToMultiPolygon($self);
1134
$self->AddGeometry($g);
1138
sub ForceToCollection {
1139
my $self = Geo::OGR::Geometry->create(GeometryType => 'GeometryCollection');
1141
$self->AddGeometry($g);
1145
*Collect = *ForceToCollection;
1149
my $n = $self->GetGeometryCount;
1151
for my $i (0..$n-1) {
1152
push @c, $self->GetGeometryRef($i)->Clone;
958
1159
*AsText = *ExportToWkt;
959
1160
*AsBinary = *ExportToWkb;
960
1161
*AsGML = *ExportToGML;
961
1162
*AsKML = *ExportToKML;
1163
*AsJSON = *ExportToJson;
1164
*BuildPolygonFromEdges = *Geo::OGR::BuildPolygonFromEdges;
1165
*ForceToPolygon = *Geo::OGR::ForceToPolygon;
963
1168
sub GeometryType {
964
1169
my($type_or_name) = @_;
985
1190
return @drivers;
988
my($name_or_number) = @_;
989
return _GetDriver($name_or_number) if $name_or_number =~ /^\d/;
990
return GetDriverByName("$name_or_number");
1194
my $driver = GetDriverByName("$name");
1195
$driver = _GetDriver($name) unless $driver;
1196
croak "No such OGR driver: $name\n" unless $driver;
992
1199
*Driver = *GetDriver;