/usr/lib/python3/dist-packages/pygeoif/geometry.py is in python3-pygeoif 0.6-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 | # -*- coding: utf-8 -*-
#
# Copyright (C) 2012 Christian Ledermann
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import re
class _GeoObject(object):
"""Base Class for Geometry, Feature, and FeatureCollection"""
def __repr__(self):
if self._type == 'Point':
return("Point({0}, {1})".format(self.x, self.y))
elif self._type == 'LineString':
instance = "LineString Instance"
qty = len(self.coords)
return "<{0} {1} Coords>".format(instance, qty)
elif self._type == 'LinearRing':
instance = "LinearRing Instance"
qty = len(self.coords)
return "<{0} {1} Coords>".format(instance, qty)
elif self._type == 'Polygon':
instance = "Polygon Instance"
inter_qty = len(self._interiors)
exter_qty = len(self._exterior.coords)
return "<{0} {1} Interior {2} Exterior>".format(
instance, inter_qty, exter_qty)
elif self._type == 'MultiPoint':
instance = "MultiPoint Instance"
qty = len(self)
return "<{0} {1} Points>".format(instance, qty)
elif self._type == 'MultiLineString':
instance = "MultiLineString Instance"
qty = len(self)
bounds = self.bounds
return "<{0} {1} Lines {2} bbox>".format(instance, qty, bounds)
elif self._type == 'MultiPolygon':
instance = "MultiPolygon Instance"
qty = len(self)
bounds = self.bounds
return "<{0} {1} Polygons {2} bbox>".format(instance, qty, bounds)
elif self._type == 'GeometryCollection':
instance = "GeometryCollection Instance"
qty = len(self)
bounds = self.bounds
return "<{0} {1} Geometries {2} bbox>".format(
instance, qty, bounds)
elif self._type == 'Feature':
instance = "Feature Instance"
geometry = self._geometry._type
properties = len(self._properties)
return "<{0} {1} geometry {2} properties>".format(instance,
geometry,
properties)
elif self._type == 'FeatureCollection':
instance = 'FeatureCollection Instance'
qty = len(self)
bounds = self.bounds
return '<{0} {1} Features {2} bbox>'.format(instance, qty, bounds)
else:
return object.__repr__(self)
class _Geometry(_GeoObject):
"""
Base Class for geometry objects.
Inherits from GeoObject
"""
_type = None
_coordinates = ()
@property
def __geo_interface__(self):
return {
'type': self._type,
'coordinates': tuple(self._coordinates)
}
def __str__(self):
return self.to_wkt()
@property
def wkt(self):
return self.to_wkt()
def to_wkt(self):
raise NotImplementedError
@property
def geom_type(self):
return self._type
@property
def bounds(self):
raise NotImplementedError
class Feature(_GeoObject):
"""
Aggregates a geometry instance with associated user-defined properties.
Attributes
~~~~~~~~~~~
geometry : object
A geometry instance
properties : dict
A dictionary linking field keys with values
associated with geometry instance
Example
~~~~~~~~
>>> p = Point(1.0, -1.0)
>>> props = {'Name': 'Sample Point', 'Other': 'Other Data'}
>>> a = Feature(p, props)
>>> a.properties
{'Name': 'Sample Point', 'Other': 'Other Data'}
>>> a.properties['Name']
'Sample Point'
"""
_type = 'Feature'
_properties = None
_geometry = None
_feature_id = None
def __init__(self, geometry, properties={}, feature_id=None, *kwargs):
self._geometry = geometry
self._properties = properties
self._feature_id = feature_id
@property
def id(self):
return self._feature_id
@property
def geometry(self):
return self._geometry
@property
def properties(self):
return self._properties
@property
def __geo_interface__(self):
geo_interface = {'type': self._type,
'geometry': self._geometry.__geo_interface__,
'properties': self._properties
}
if self._feature_id is None:
return geo_interface
else:
geo_interface['id'] = self._feature_id
return geo_interface
class Point(_Geometry):
"""
A zero dimensional geometry
A point has zero length and zero area.
Attributes
----------
x, y, z : float
Coordinate values
Example
-------
>>> p = Point(1.0, -1.0)
>>> print p
POINT (1.0000000000000000 -1.0000000000000000)
>>> p.y
-1.0
>>> p.x
1.0
"""
_type = 'Point'
_coordinates = None
def __init__(self, *args):
"""
Parameters
----------
There are 2 cases:
1) 1 parameter: this must satisfy the __geo_interface__ protocol
or be a tuple or list of x, y, [z]
2) 2 or 3 parameters: x, y, [z] : float
Easting, northing, and elevation.
"""
self._coordinates = ()
if len(args) == 1:
if hasattr(args[0], '__geo_interface__'):
if args[0].__geo_interface__['type'] == 'Point':
self._coordinates = list(
args[0].__geo_interface__['coordinates']
)
else:
raise TypeError
else:
if isinstance(args[0], (list, tuple)):
if 2 <= len(args[0]) <= 3:
coords = [float(x) for x in args[0]]
self._coordinates = coords
else:
raise TypeError
else:
raise TypeError
elif 2 <= len(args) <= 3:
coords = [float(x) for x in args]
self._coordinates = coords
else:
raise ValueError
@property
def x(self):
"""Return x coordinate."""
return self._coordinates[0]
@property
def y(self):
"""Return y coordinate."""
return self._coordinates[1]
@property
def z(self):
"""Return z coordinate."""
if len(self._coordinates) != 3:
raise ValueError("This point has no z coordinate.")
return self._coordinates[2]
@property
def coords(self):
return (tuple(self._coordinates),)
@coords.setter
def coords(self, coordinates):
if isinstance(coordinates, (list, tuple)):
if 2 <= len(coordinates) <= 3:
coords = [float(x) for x in coordinates]
self._coordinates = coords
else:
raise TypeError
else:
raise TypeError
@property
def bounds(self):
return tuple(self._coordinates + self._coordinates)
def to_wkt(self):
coords = str(tuple(self._coordinates)).replace(',', '')
return self._type.upper() + ' ' + coords
class LineString(_Geometry):
"""
A one-dimensional figure comprising one or more line segments
A LineString has non-zero length and zero area. It may approximate a curve
and need not be straight. Unlike a LinearRing, a LineString is not closed.
Attributes
----------
geoms : sequence
A sequence of Points
"""
_type = 'LineString'
_geoms = None
@property
def __geo_interface__(self):
if self._type and self._geoms:
return {
'type': self._type,
'coordinates': tuple(self.coords)
}
def __init__(self, coordinates):
"""
Parameters
----------
coordinates : sequence
A sequence of (x, y [,z]) numeric coordinate pairs or triples
or a sequence of Points or
an object that provides the __geo_interface__, including another
instance of LineString.
Example
-------
Create a line with two segments
>>> a = LineString([[0, 0], [1, 0], [1, 1]])
"""
self._geoms = []
if hasattr(coordinates, '__geo_interface__'):
gi = coordinates.__geo_interface__
if (gi['type'] == 'LineString') or (gi['type'] == 'LinearRing'):
self.coords = gi['coordinates']
elif gi['type'] == 'Polygon':
raise TypeError('Use poligon.exterior or polygon.interiors[x]')
else:
raise TypeError
elif isinstance(coordinates, (list, tuple)):
geoms = []
for coord in coordinates:
p = Point(coord)
l = len(p.coords[0])
if geoms:
if l != l2:
raise ValueError
l2 = l
geoms.append(p)
self._geoms = geoms
else:
raise TypeError
@property
def geoms(self):
return tuple(self._geoms)
@property
def coords(self):
coordinates = []
for point in self.geoms:
coordinates.append(tuple(point.coords[0]))
return tuple(coordinates)
@coords.setter
def coords(self, coordinates):
if isinstance(coordinates, (list, tuple)):
geoms = []
for coord in coordinates:
p = Point(coord)
l = len(p.coords[0])
if geoms:
if l != l2:
raise ValueError
l2 = l
geoms.append(p)
self._geoms = geoms
else:
raise ValueError
def to_wkt(self):
wc = [' '.join([str(x) for x in c]) for c in self.coords]
return self._type.upper() + ' (' + ', '.join(wc) + ')'
@property
def bounds(self):
if self.coords:
minx = self.coords[0][0]
miny = self.coords[0][1]
maxx = self.coords[0][0]
maxy = self.coords[0][1]
for coord in self.coords:
minx = min(coord[0], minx)
miny = min(coord[1], miny)
maxx = max(coord[0], maxx)
maxy = max(coord[1], maxy)
return (minx, miny, maxx, maxy)
class LinearRing(LineString):
"""
A closed one-dimensional geometry comprising one or more line segments
A LinearRing that crosses itself or touches itself at a single point is
invalid and operations on it may fail.
A Linear Ring is self closing
"""
_type = 'LinearRing'
def __init__(self, coordinates=None):
super(LinearRing, self).__init__(coordinates)
if self._geoms[0].coords != self._geoms[-1].coords:
self._geoms.append(self._geoms[0])
@property
def coords(self):
if self._geoms[0].coords == self._geoms[-1].coords:
coordinates = []
for point in self.geoms:
coordinates.append(tuple(point.coords[0]))
return tuple(coordinates)
else:
raise ValueError
@coords.setter
def coords(self, coordinates):
LineString.coords.fset(self, coordinates)
if self._geoms[0].coords != self._geoms[-1].coords:
self._geoms.append(self._geoms[0])
def _set_orientation(self, clockwise=False):
""" sets the orientation of the coordinates in
clockwise or counterclockwise (default) order"""
area = signed_area(self.coords)
if (area >= 0) and clockwise:
self._geoms = self._geoms[::-1]
elif (area < 0) and not clockwise:
self._geoms = self._geoms[::-1]
class Polygon(_Geometry):
"""
A two-dimensional figure bounded by a linear ring
A polygon has a non-zero area. It may have one or more negative-space
"holes" which are also bounded by linear rings. If any rings cross each
other, the geometry is invalid and operations on it may fail.
Attributes
----------
exterior : LinearRing
The ring which bounds the positive space of the polygon.
interiors : sequence
A sequence of rings which bound all existing holes.
"""
_type = 'Polygon'
_exterior = None
_interiors = None
@property
def __geo_interface__(self):
if self._interiors:
coords = [self.exterior.coords]
for hole in self.interiors:
coords.append(hole.coords)
return {
'type': self._type,
'coordinates': tuple(coords)
}
elif self._exterior:
return {
'type': self._type,
'coordinates': (self._exterior.coords,)
}
def __init__(self, shell, holes=None):
"""
Parameters
----------
shell : sequence
A sequence of (x, y [,z]) numeric coordinate pairs or triples
or a LinearRing.
If a Polygon is passed as shell the holes parameter will be
ignored
holes : sequence
A sequence of objects which satisfy the same requirements as the
shell parameters above
Example
-------
Create a square polygon with no holes
>>> coords = ((0., 0.), (0., 1.), (1., 1.), (1., 0.), (0., 0.))
>>> polygon = Polygon(coords)
"""
if holes:
self._interiors = []
for hole in holes:
if hasattr(hole, '__geo_interface__'):
gi = hole.__geo_interface__
if gi['type'] == 'LinearRing':
self._interiors.append(LinearRing(hole))
else:
raise TypeError
elif isinstance(hole, (list, tuple)):
self._interiors.append(LinearRing(hole))
else:
self._interiors = []
if hasattr(shell, '__geo_interface__'):
gi = shell.__geo_interface__
if gi['type'] == 'LinearRing':
self._exterior = LinearRing(shell)
elif gi['type'] == 'Polygon':
self._exterior = LinearRing(gi['coordinates'][0])
if len(gi['coordinates']) > 1:
# XXX should the holes passed if any be ignored
# or added to the polygon?
self._interiors = []
for hole in gi['coordinates'][1:]:
self._interiors.append(LinearRing(hole))
else:
raise TypeError
elif isinstance(shell, (list, tuple)):
assert isinstance(shell[0], (list, tuple))
if isinstance(shell[0][0], (list, tuple)):
# we passed shell and holes in the first parameter
self._exterior = LinearRing(shell[0])
if len(shell) > 1:
for hole in shell[1:]:
self._interiors.append(LinearRing(hole))
else:
self._exterior = LinearRing(shell)
else:
raise TypeError
@property
def exterior(self):
if self._exterior is not None:
return self._exterior
@property
def interiors(self):
if self._exterior is not None:
if self._interiors:
for interior in self._interiors:
yield interior
@property
def bounds(self):
if self.exterior:
return self.exterior.bounds
def to_wkt(self):
ext = [' '.join([str(x) for x in c]) for c in self.exterior.coords]
ec = ('(' + ', '.join(ext) + ')')
ic = ''
for interior in self.interiors:
ic += ',(' + ', '.join(
[' '.join([str(x) for x in c]) for c in interior.coords]
) + ')'
return self._type.upper() + '(' + ec + ic + ')'
def _set_orientation(self, clockwise=False, exterior=True, interiors=True):
""" sets the orientation of the coordinates in
clockwise or counterclockwise (default) order"""
if exterior:
self.exterior._set_orientation(clockwise)
if interiors:
for interior in self.interiors:
interior._set_orientation(clockwise)
class MultiPoint(_Geometry):
"""A collection of one or more points
Attributes
----------
geoms : sequence
A sequence of Points
"""
_geoms = None
_type = 'MultiPoint'
@property
def __geo_interface__(self):
return {
'type': self._type,
'coordinates': tuple([g.coords[0] for g in self._geoms])
}
def __init__(self, points):
"""
Parameters
----------
points : sequence
A sequence of (x, y [,z]) numeric coordinate pairs or triples or a
sequence of objects that implement the __geo_interface__,
including instaces of Point.
Example
-------
Construct a 2 point collection
>>> ob = MultiPoint([[0.0, 0.0], [1.0, 2.0]])
>>> len(ob.geoms)
2
>>> type(ob.geoms[0]) == Point
True
"""
self._geoms = []
if isinstance(points, (list, tuple)):
for point in points:
if hasattr(point, '__geo_interface__'):
self._from_geo_interface(point)
elif isinstance(point, (list, tuple)):
p = Point(point)
self._geoms.append(p)
else:
raise TypeError
elif hasattr(points, '__geo_interface__'):
self._from_geo_interface(points)
else:
raise TypeError
def _from_geo_interface(self, point):
gi = point.__geo_interface__
if gi['type'] == 'Point':
p = Point(point)
self._geoms.append(p)
elif gi['type'] == 'LinearRing' or gi['type'] == 'LineString':
l = LineString(point)
for coord in l.coords:
p = Point(coord)
self._geoms.append(p)
elif gi['type'] == 'Polygon':
p = Polygon(point)
for coord in p.exterior.coords:
p1 = Point(coord)
self._geoms.append(p1)
for interior in p.interiors:
for coord in interior.coords:
p1 = Point(coord)
self._geoms.append(p1)
else:
raise TypeError
@property
def geoms(self):
return tuple(self._geoms)
@property
def bounds(self):
if self._geoms:
minx = self.geoms[0].coords[0][0]
miny = self.geoms[0].coords[0][1]
maxx = self.geoms[0].coords[0][0]
maxy = self.geoms[0].coords[0][1]
for geom in self.geoms:
minx = min(geom.coords[0][0], minx)
miny = min(geom.coords[0][1], miny)
maxx = max(geom.coords[0][0], maxx)
maxy = max(geom.coords[0][1], maxy)
return (minx, miny, maxx, maxy)
def unique(self):
""" Make Points unique, delete duplicates """
coords = [geom.coords for geom in self.geoms]
self._geoms = [Point(coord[0]) for coord in set(coords)]
def to_wkt(self):
wc = [' '.join([str(x) for x in c.coords[0]]) for c in self.geoms]
return self._type.upper() + '(' + ', '.join(wc) + ')'
def __len__(self):
if self._geoms:
return len(self._geoms)
else:
return 0
class MultiLineString(_Geometry):
"""
A collection of one or more line strings
A MultiLineString has non-zero length and zero area.
Attributes
----------
geoms : sequence
A sequence of LineStrings
"""
_geoms = None
_type = 'MultiLineString'
@property
def __geo_interface__(self):
return {
'type': self._type,
'coordinates': tuple(
tuple(c for c in g.coords) for g in self.geoms
)
}
def __init__(self, lines):
"""
Parameters
----------
lines : sequence
A sequence of line-like coordinate sequences or objects that
provide the __geo_interface__, including instances of
LineString.
Example
-------
Construct a collection containing one line string.
>>> lines = MultiLineString( [[[0.0, 0.0], [1.0, 2.0]]] )
"""
self._geoms = []
if isinstance(lines, (list, tuple)):
for line in lines:
l = LineString(line)
self._geoms.append(l)
elif hasattr(lines, '__geo_interface__'):
gi = lines.__geo_interface__
if gi['type'] == 'LinearRing' or gi['type'] == 'LineString':
l = LineString(gi['coordinates'])
self._geoms.append(l)
elif gi['type'] == 'MultiLineString':
for line in gi['coordinates']:
l = LineString(line)
self._geoms.append(l)
else:
raise TypeError
else:
raise TypeError
@property
def geoms(self):
return tuple(self._geoms)
@property
def bounds(self):
if self._geoms:
minx = self.geoms[0].bounds[0]
miny = self.geoms[0].bounds[1]
maxx = self.geoms[0].bounds[2]
maxy = self.geoms[0].bounds[3]
for geom in self.geoms:
minx = min(geom.bounds[0], minx)
miny = min(geom.bounds[1], miny)
maxx = max(geom.bounds[2], maxx)
maxy = max(geom.bounds[3], maxy)
return (minx, miny, maxx, maxy)
def to_wkt(self):
wc = '(' + ', '.join(
[' '.join([str(x) for x in c]) for c in self.geoms[0].coords]
) + ')'
for lx in self.geoms[1:]:
wc += ',(' + ', '.join(
[' '.join([str(x) for x in c]) for c in lx.coords]
) + ')'
return self._type.upper() + '(' + wc + ')'
def __len__(self):
if self._geoms:
return len(self._geoms)
else:
return 0
class MultiPolygon(_Geometry):
"""A collection of one or more polygons
If component polygons overlap the collection is `invalid` and some
operations on it may fail.
Attributes
----------
geoms : sequence
A sequence of `Polygon` instances
"""
_geoms = None
_type = 'MultiPolygon'
@property
def __geo_interface__(self):
allcoords = []
for geom in self.geoms:
coords = []
coords.append(tuple(geom.exterior.coords))
for hole in geom.interiors:
coords.append(tuple(hole.coords))
allcoords.append(tuple(coords))
return {
'type': self._type,
'coordinates': tuple(allcoords)
}
def __init__(self, polygons):
"""
Parameters
----------
polygons : sequence
A sequence of (shell, holes) tuples where shell is the sequence
representation of a linear ring (see linearring.py) and holes is
a sequence of such linear rings
Example
-------
Construct a collection from a sequence of coordinate tuples
>>> ob = MultiPolygon([
... (
... ((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)),
... [((0.1, 0.1), (0.1, 0.2), (0.2, 0.2), (0.2, 0.1))]
...)
...])
>>> len(ob.geoms)
1
>>> type(ob.geoms[0]) == Polygon
True
"""
self._geoms = []
if isinstance(polygons, (list, tuple)):
for polygon in polygons:
if isinstance(polygon, (list, tuple)):
p = Polygon(polygon[0], polygon[1])
self._geoms.append(p)
elif hasattr(polygon, '__geo_interface__'):
gi = polygon.__geo_interface__
p = Polygon(polygon)
self._geoms.append(p)
else:
raise ValueError
elif hasattr(polygons, '__geo_interface__'):
gi = polygons.__geo_interface__
if gi['type'] == 'Polygon':
p = Polygon(polygons)
self._geoms.append(p)
elif gi['type'] == 'MultiPolygon':
for coords in gi['coordinates']:
self._geoms.append(Polygon(coords[0], coords[1:]))
else:
raise TypeError
else:
raise ValueError
@property
def geoms(self):
return tuple(self._geoms)
@property
def bounds(self):
if self._geoms:
minx = self.geoms[0].bounds[0]
miny = self.geoms[0].bounds[1]
maxx = self.geoms[0].bounds[2]
maxy = self.geoms[0].bounds[3]
for geom in self.geoms:
minx = min(geom.bounds[0], minx)
miny = min(geom.bounds[1], miny)
maxx = max(geom.bounds[2], maxx)
maxy = max(geom.bounds[3], maxy)
return (minx, miny, maxx, maxy)
def to_wkt(self):
pc = ''
for geom in self.geoms:
ec = '(' + ', '.join(
[' '.join([str(x) for x in c]) for c in geom.exterior.coords]
) + ')'
ic = ''
for interior in geom.interiors:
ic += ',(' + ', '.join(
[' '.join([str(x) for x in c]) for c in interior.coords]
) + ')'
pc += '(' + ec + ic + ')'
return self._type.upper() + '(' + pc + ')'
def _set_orientation(self, clockwise=False, exterior=True, interiors=True):
""" sets the orientation of the coordinates in
clockwise or counterclockwise (default) order for all
contained polygons"""
for geom in self.geoms:
geom._set_orientation(clockwise, exterior, interiors)
def __len__(self):
if self._geoms:
return len(self._geoms)
else:
return 0
class GeometryCollection(_Geometry):
"""A heterogenous collection of geometries (Points, LineStrings,
LinearRings, and Polygons)
Attributes
----------
geoms : sequence
A sequence of geometry instances
Please note:
GEOMETRYCOLLECTION isn't supported by the Shapefile format. And this sub-
class isn't generally supported by ordinary GIS sw (viewers and so on). So
it's very rarely used in the real GIS professional world.
Example
-------
Initialize Geometries and construct a GeometryCollection
>>> from pygeoif import geometry
>>> p = geometry.Point(1.0, -1.0)
>>> p2 = geometry.Point(1.0, -1.0)
>>> geoms = [p, p2]
>>> c = geometry.GeometryCollection(geoms)
>>> c.__geo_interface__
{'type': 'GeometryCollection',
'geometries': [{'type': 'Point', 'coordinates': (1.0, -1.0)},
{'type': 'Point', 'coordinates': (1.0, -1.0)}]}
"""
_type = 'GeometryCollection'
_geoms = None
_allowed_geomtries = (Point, LineString, LinearRing, Polygon)
@property
def __geo_interface__(self):
gifs = []
for geom in self._geoms:
gifs.append(geom.__geo_interface__)
return {'type': self._type, 'geometries': gifs}
def __init__(self, geometries):
self._geoms = []
if isinstance(geometries, (list, tuple)):
for geometry in geometries:
if isinstance(geometry, self._allowed_geomtries):
self._geoms.append(geometry)
elif isinstance(as_shape(geometry), self._allowed_geomtries):
self._geoms.append(as_shape(geometry))
else:
raise ValueError
else:
raise TypeError
@property
def geoms(self):
for geom in self._geoms:
if isinstance(geom, self._allowed_geomtries):
yield geom
else:
raise ValueError("Illegal geometry type.")
@property
def bounds(self):
if self._geoms:
minx = self._geoms[0].bounds[0]
miny = self._geoms[0].bounds[1]
maxx = self._geoms[0].bounds[2]
maxy = self._geoms[0].bounds[3]
for geom in self.geoms:
minx = min(geom.bounds[0], minx)
miny = min(geom.bounds[1], miny)
maxx = max(geom.bounds[2], maxx)
maxy = max(geom.bounds[3], maxy)
return (minx, miny, maxx, maxy)
def to_wkt(self):
wkts = []
for geom in self.geoms:
wkts.append(geom.to_wkt())
return 'GEOMETRYCOLLECTION (%s)' % ', '.join(wkts)
def __len__(self):
if self._geoms:
return len(self._geoms)
else:
return 0
def __iter__(self):
return iter(self._geoms)
class FeatureCollection(_GeoObject):
"""A heterogenous collection of Features
Attributes
----------
features : sequence
A sequence of feature instances
Example
-------
>>> from pygeoif import geometry
>>> p = geometry.Point(1.0, -1.0)
>>> props = {'Name': 'Sample Point', 'Other': 'Other Data'}
>>> a = geometry.Feature(p, props)
>>> p2 = geometry.Point(1.0, -1.0)
>>> props2 = {'Name': 'Sample Point2', 'Other': 'Other Data2'}
>>> b = geometry.Feature(p2, props2)
>>> features = [a, b]
>>> c = geometry.FeatureCollection(features)
>>> c.__geo_interface__
{'type': 'FeatureCollection',
'features': [{'geometry': {'type': 'Point', 'coordinates': (1.0, -1.0)},
'type': 'Feature',
'properties': {'Other': 'Other Data', 'Name': 'Sample Point'}},
{'geometry': {'type': 'Point', 'coordinates': (1.0, -1.0)},
'type': 'Feature',
'properties': {'Other': 'Other Data2', 'Name': 'Sample Point2'}}]}
"""
_type = 'FeatureCollection'
_features = None
@property
def __geo_interface__(self):
gifs = []
for feature in self._features:
gifs.append(feature.__geo_interface__)
return {'type': self._type, 'features': gifs}
def __init__(self, features):
self._features = []
if isinstance(features, (list, tuple)):
for feature in features:
if isinstance(feature, Feature):
self._features.append(feature)
else:
raise ValueError
else:
raise TypeError
@property
def features(self):
for feature in self._features:
if isinstance(feature, Feature):
yield feature
else:
raise ValueError("Illegal type.")
@property
def bounds(self):
if self._features:
minx = self._features[0].geometry.bounds[0]
miny = self._features[0].geometry.bounds[1]
maxx = self._features[0].geometry.bounds[2]
maxy = self._features[0].geometry.bounds[3]
for feature in self.features:
minx = min(feature.geometry.bounds[0], minx)
miny = min(feature.geometry.bounds[1], miny)
maxx = max(feature.geometry.bounds[2], maxx)
maxy = max(feature.geometry.bounds[3], maxy)
return (minx, miny, maxx, maxy)
def __len__(self):
if self._features:
return len(self._features)
else:
return 0
def __iter__(self):
return iter(self._features)
def signed_area(coords):
"""Return the signed area enclosed by a ring using the linear time
algorithm at http://www.cgafaq.info/wiki/Polygon_Area. A value >= 0
indicates a counter-clockwise oriented ring.
"""
if len(coords[0]) == 2:
xs, ys = map(list, zip(*coords))
elif len(coords[0]) == 3:
xs, ys, zs = map(list, zip(*coords))
else:
raise ValueError
xs.append(xs[1])
ys.append(ys[1])
return sum(xs[i]*(ys[i+1]-ys[i-1]) for i in range(1, len(coords)))/2.0
def orient(polygon, sign=1.0):
s = float(sign)
rings = []
ring = polygon.exterior
if signed_area(ring.coords)/s >= 0.0:
rings.append(ring.coords)
else:
rings.append(list(ring.coords)[::-1])
for ring in polygon.interiors:
if signed_area(ring.coords)/s <= 0.0:
rings.append(ring.coords)
else:
rings.append(list(ring.coords)[::-1])
return Polygon(rings[0], rings[1:])
def as_shape(geometry):
""" creates a pygeoif geometry from an object that
provides the __geo_interface__ or a dictionary that
is __geo_interface__ compatible"""
gi = None
if isinstance(geometry, dict):
is_geometryCollection = geometry['type'] == 'GeometryCollection'
is_feature = geometry['type'] == 'Feature'
if ('coordinates' in geometry) and ('type' in geometry):
gi = geometry
elif is_geometryCollection and 'geometries' in geometry:
gi = geometry
elif is_feature:
gi = geometry
elif hasattr(geometry, '__geo_interface__'):
gi = geometry.__geo_interface__
else:
try:
# maybe we can convert it into a valid __geo_interface__ dict
cdict = dict(geometry)
is_geometryCollection = cdict['type'] == 'GeometryCollection'
if ('coordinates' in cdict) and ('type' in cdict):
gi = cdict
elif is_geometryCollection and 'geometries' in cdict:
gi = cdict
except:
pass
if gi:
ft = gi['type']
if ft == 'GeometryCollection':
geometries = []
for fi in gi['geometries']:
geometries.append(as_shape(fi))
return GeometryCollection(geometries)
if ft == 'Feature':
return Feature(as_shape(gi['geometry']),
properties=gi.get('properties', {}),
feature_id=gi.get('id', None))
if ft == 'FeatureCollection':
features = []
for fi in gi['features']:
features.append(as_shape(fi))
return FeatureCollection(features)
coords = gi['coordinates']
if ft == 'Point':
return Point(coords)
elif ft == 'LineString':
return LineString(coords)
elif ft == 'LinearRing':
return LinearRing(coords)
elif ft == 'Polygon':
return Polygon(coords)
elif ft == 'MultiPoint':
return MultiPoint(coords)
elif ft == 'MultiLineString':
return MultiLineString(coords)
elif ft == 'MultiPolygon':
polygons = []
for icoords in coords:
polygons.append(Polygon(icoords[0], icoords[1:]))
return MultiPolygon(polygons)
else:
raise NotImplementedError
else:
raise TypeError('Object does not implement __geo_interface__')
wkt_regex = re.compile(r'^(SRID=(?P<srid>\d+);)?'
r'(?P<wkt>'
r'(?P<type>POINT|LINESTRING|LINEARRING|POLYGON|'
r'MULTIPOINT|MULTILINESTRING|MULTIPOLYGON|'
r'GEOMETRYCOLLECTION)'
r'[ACEGIMLONPSRUTYZ\d,\.\-\(\) ]+)$',
re.I
)
gcre = re.compile(r'POINT|LINESTRING|LINEARRING|POLYGON')
outer = re.compile("\((.+)\)")
inner = re.compile("\([^)]*\)")
mpre = re.compile("\(\((.+?)\)\)")
def from_wkt(geo_str):
"""
Create a geometry from its WKT representation
"""
wkt = geo_str.strip()
wkt = ' '.join([l.strip() for l in wkt.splitlines()])
wkt = wkt_regex.match(wkt).group('wkt')
ftype = wkt_regex.match(wkt).group('type')
outerstr = outer.search(wkt)
coordinates = outerstr.group(1)
if ftype == 'POINT':
coords = coordinates.split()
return Point(coords)
elif ftype == 'LINESTRING':
coords = coordinates.split(',')
return LineString([c.split() for c in coords])
elif ftype == 'LINEARRING':
coords = coordinates.split(',')
return LinearRing([c.split() for c in coords])
elif ftype == 'POLYGON':
coords = []
for interior in inner.findall(coordinates):
coords.append((interior[1:-1]).split(','))
if len(coords) > 1:
# we have a polygon with holes
exteriors = []
for ext in coords[1:]:
exteriors.append([c.split() for c in ext])
else:
exteriors = None
return Polygon([c.split() for c in coords[0]], exteriors)
elif ftype == 'MULTIPOINT':
coords1 = coordinates.split(',')
coords = []
for coord in coords1:
if '(' in coord:
coord = coord[coord.find('(') + 1: coord.rfind(')')]
coords.append(coord.strip())
return MultiPoint([c.split() for c in coords])
elif ftype == 'MULTILINESTRING':
coords = []
for lines in inner.findall(coordinates):
coords.append([c.split() for c in lines[1:-1].split(',')])
return MultiLineString(coords)
elif ftype == 'MULTIPOLYGON':
polygons = []
m = mpre.split(coordinates)
for polygon in m:
if len(polygon) < 3:
continue
coords = []
for interior in inner.findall('(' + polygon + ')'):
coords.append((interior[1:-1]).split(','))
if len(coords) > 1:
# we have a polygon with holes
exteriors = []
for ext in coords[1:]:
exteriors.append([c.split() for c in ext])
else:
exteriors = None
polygons.append(Polygon([c.split() for c in coords[0]], exteriors))
return MultiPolygon(polygons)
elif ftype == 'GEOMETRYCOLLECTION':
gc_types = gcre.findall(coordinates)
gc_coords = gcre.split(coordinates)[1:]
assert(len(gc_types) == len(gc_coords))
geometries = []
for (gc_type, gc_coord) in zip(gc_types, gc_coords):
gc_wkt = gc_type + gc_coord[:gc_coord.rfind(')') + 1]
geometries.append(from_wkt(gc_wkt))
return GeometryCollection(geometries)
else:
raise NotImplementedError
def mapping(ob):
return ob.__geo_interface__
|