8000 Rearranged imports in geos files · ddriddle/django@b695153 · GitHub
[go: up one dir, main page]

Skip to content

Commit b695153

Browse files
committed
Rearranged imports in geos files
1 parent d9bcba9 commit b695153

File tree

6 files changed

+58
-57
lines changed

6 files changed

+58
-57
lines changed

django/contrib/gis/geos/collections.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
from ctypes import byref, c_int, c_uint
66

77
from django.contrib.gis.geos import prototypes as capi
8-
from django.contrib.gis.geos.geometry import GEOSGeometry
8+
from django.contrib.gis.geos.geometry import (
9+
GEOSGeometry, ProjectInterpolateMixin,
10+
)
911
from django.contrib.gis.geos.libgeos import get_pointer_arr
1012
from django.contrib.gis.geos.linestring import LinearRing, LineString
1113
from django.contrib.gis.geos.point import Point
@@ -99,7 +101,7 @@ class MultiPoint(GeometryCollection):
99101
_typeid = 4
100102

101103

102-
class MultiLineString(GeometryCollection):
104+
class MultiLineString(ProjectInterpolateMixin, GeometryCollection):
103105
_allowed = (LineString, LinearRing)
104106
_typeid = 5
105107

django/contrib/gis/geos/geometry.py

Lines changed: 44 additions & 52 deletions
8000
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from django.contrib.gis.geos.error import GEOSException, GEOSIndexError
1515
from django.contrib.gis.geos.libgeos import GEOM_PTR
1616
from django.contrib.gis.geos.mutable_list import ListMixin
17+
from django.contrib.gis.geos.prepared import PreparedGeometry
1718
from django.contrib.gis.geos.prototypes.io import (
1819
ewkb_w, wkb_r, wkb_w, wkt_r, wkt_w,
1920
)
@@ -27,8 +28,10 @@ class GEOSGeometry(GEOSBase, ListMixin):
2728
# Raise GEOSIndexError instead of plain IndexError
2829
# (see ticket #4740 and GEOSIndexError docstring)
2930
_IndexError = GEOSIndexError
31+
_GEOS_CLASSES = None
3032

3133
ptr_type = GEOM_PTR
34+
has_cs = False # Only Point, LineString, LinearRing have coordinate sequences
3235

3336
def __init__(self, geo_input, srid=None):
3437
"""
@@ -92,7 +95,24 @@ def _post_init(self, srid):
9295
self.srid = srid
9396

9497
# Setting the class type (e.g., Point, Polygon, etc.)
95-
self.__class__ = GEOS_CLASSES[self.geom_typeid]
98+
if GEOSGeometry._GEOS_CLASSES is None:
99+
# Lazy-loaded variable to avoid import conflicts with GEOSGeometry.
100+
from .linestring import LineString, LinearRing
8000 101+
from .point import Point
102+
from .polygon import Polygon
103+
from .collections import (
104+
GeometryCollection, MultiPoint, MultiLineString, MultiPolygon)
105+
GEOSGeometry._GEOS_CLASSES = {
106+
0: Point,
107+
1: LineString,
108+
2: LinearRing,
109+
3: Polygon,
110+
4: MultiPoint,
111+
5: MultiLineString,
112+
6: MultiPolygon,
113+
7: GeometryCollection,
114+
}
115+
self.__class__ = GEOSGeometry._GEOS_CLASSES[self.geom_typeid]
96116

97117
# Setting the coordinate sequence for the geometry (will be None on
98118
# geometries that do not have coordinate sequences)
@@ -185,15 +205,6 @@ def __xor__(self, other):
185205
return self.sym_difference(other)
186206

187207
# #### Coordinate Sequence Routines ####
188-
@property
189-
def has_cs(self):
190-
"Returns True if this Geometry has a coordinate sequence, False if not."
191-
# Only these geometries are allowed to have coordinate sequences.
192-
if isinstance(self, (Point, LineString, LinearRing)):
193-
return True
194-
else:
195-
return False
196-
197208
def _set_cs(self):
198209
"Sets the coordinate sequence for this Geometry."
199210
if self.has_cs:
@@ -560,16 +571,6 @@ def envelope(self):
560571
"Return the envelope for this geometry (a polygon)."
561572
return self._topology(capi.geos_envelope(self.ptr))
562573

563-
def interpolate(self, distance):
564-
if not isinstance(self, (LineString, MultiLineString)):
565-
raise TypeError('interpolate only works on LineString and MultiLineString geometries')
566-
return self._topology(capi.geos_interpolate(self.ptr, distance))
567-
568-
def interpolate_normalized(self, distance):
569-
if not isinstance(self, (LineString, MultiLineString)):
570-
raise TypeError('interpolate only works on LineString and MultiLineString geometries')
571-
return self._topology(capi.geos_interpolate_normalized(self.ptr, distance))
572-
573574
def intersection(self, other):
574575
"Returns a Geometry representing the points shared by this Geometry and other."
575576
return self._topology(capi.geos_intersection(self.ptr, other.ptr))
@@ -579,20 +580,6 @@ def point_on_surface(self):
579580
"Computes an interior point of this Geometry."
580581
return self._topology(capi.geos_pointonsurface(self.ptr))
581582

582-
def project(self, point):
583-
if not isinstance(point, Point):
584-
raise TypeError('locate_point argument must be a Point')
585-
if not isinstance(self, (LineString, MultiLineString)):
586-
raise TypeError('locate_point only works on LineString and MultiLineString geometries')
587-
return capi.geos_project(self.ptr, point.ptr)
588-
589-
def project_normalized(self, point):
590-
if not isinstance(point, Point):
591-
raise TypeError('locate_point argument must be a Point')
592-
if not isinstance(self, (LineString, MultiLineString)):
593-
raise TypeError('locate_point only works on LineString and MultiLineString geometries')
594-
return capi.geos_project_normalized(self.ptr, point.ptr)
595-
596583
def relate(self, other):
597584
"Returns the DE-9IM intersection matrix for this Geometry and the other."
598585
return capi.geos_relate(self.ptr, other.ptr).decode()
@@ -647,6 +634,7 @@ def extent(self):
647634
Returns the extent of this geometry as a 4-tuple, consisting of
648635
(xmin, ymin, xmax, ymax).
649636
"""
637+
from .point import Point
650638
env = self.envelope
651639
if isinstance(env, Point):
652640
xmin, ymin = env.tuple
@@ -668,21 +656,25 @@ def clone(self):
668656
"Clones this Geometry."
669657
return GEOSGeometry(capi.geom_clone(self.ptr), srid=self.srid)
670658

671-
# Class mapping dictionary. Has to be at the end to avoid import
672-
# conflicts with GEOSGeometry.
673-
from django.contrib.gis.geos.linestring import LineString, LinearRing # isort:skip
674-
from django.contrib.gis.geos.point import Point # isort:skip
675-
from django.contrib.gis.geos.polygon import Polygon # isort:skip
676-
from django.contrib.gis.geos.collections import ( # isort:skip
677-
GeometryCollection, MultiPoint, MultiLineString, MultiPolygon)
678-
from django.contrib.gis.geos.prepared import PreparedGeometry # isort:skip
679-
GEOS_CLASSES = {
680-
0: Point,
681-
1: LineString,
682-
2: LinearRing,
683-
3: Polygon,
684-
4: MultiPoint,
685-
5: MultiLineString,
686-
6: MultiPolygon,
687-
7: GeometryCollection,
688-
}
659+
660+
class ProjectInterpolateMixin(object):
661+
"""
662+
Used for LineString and MultiLineString.
663+
"""
664+
def interpolate(self, distance):
665+
return self._topology(capi.geos_interpolate(self.ptr, distance))
666+
667+
def interpolate_normalized(self, distance):
668+
return self._topology(capi.geos_interpolate_normalized(self.ptr, distance))
669+
670+
def project(self, point):
671+
from .point import Point
672+
if not isinstance(point, Point):
673+
raise TypeError('locate_point argument must be a Point')
674+
return capi.geos_project(self.ptr, point.ptr)
675+
676+
def project_normalized(self, point):
677+
from .point import Point
678+
if not isinstance(point, Point):
679+
raise TypeError('locate_point argument must be a Point')
680+
return capi.geos_project_normalized(self.ptr, point.ptr)

django/contrib/gis/geos/libgeos.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ def get_pointer_arr(n):
135135

136136

137137
class GEOSFuncFactory(object):
138+
"""
139+
Lazy loading of GEOS functions.
140+
"""
138141
argtypes = None
139142
restype = None
140143
errcheck = None

django/contrib/gis/geos/linestring.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
from django.contrib.gis.geos import prototypes as capi
22
from django.contrib.gis.geos.coordseq import GEOSCoordSeq
33
from django.contrib.gis.geos.error import GEOSException
4-
from django.contrib.gis.geos.geometry import GEOSGeometry
4+
from django.contrib.gis.geos.geometry import (
5+
GEOSGeometry, ProjectInterpolateMixin,
6+
)
57
from django.contrib.gis.geos.point import Point
68
from django.contrib.gis.shortcuts import numpy
79
from django.utils.six.moves import range
810

911

10-
class LineString(GEOSGeometry):
12+
class LineString(ProjectInterpolateMixin, GEOSGeometry):
1113
_init_func = capi.create_linestring
1214
_minlength = 2
15+
has_cs = True
1316

1417
def __init__(self, *args, **kwargs):
1518
"""

django/contrib/gis/geos/point.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
class Point(GEOSGeometry):
1111
_minlength = 2
1212
_maxlength = 3
13+
has_cs = True
1314

1415
def __init__(self, x, y=None, z=None, srid=None):
1516
"""

django/contrib/gis/geos/prepared.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from .base import GEOSBase
22
from .error import GEOSException
3-
from .geometry import GEOSGeometry
43
from .libgeos import geos_version_info
54
from .prototypes import prepared as capi
65

@@ -18,6 +17,7 @@ def __init__(self, geom):
1817
# from being garbage collected which could then crash the prepared one
1918
# See #21662
2019
self._base_geom = geom
20+
from .geometry import GEOSGeometry
2121
if not isinstance(geom, GEOSGeometry):
2222
raise TypeError
2323
self.ptr = capi.geos_prepare(geom.ptr)

0 commit comments

Comments
 (0)
0