Author: jbronn
Date: 2007-05-31 18:29:16 -0500 (Thu, 31 May 2007)
New Revision: 5395
Modified:
django/branches/gis/django/contrib/gis/geos/GEOSGeometry.py
django/branches/gis/django/contrib/gis/tests/geometries.py
django/branches/gis/django/contrib/gis/tests/test_geos.py
Log:
gis: Some GEOSGeometry patches, biggest change is that __iter__, __getitem__,
and __len__ now supported on Polygon and LineString
Modified: django/branches/gis/django/contrib/gis/geos/GEOSGeometry.py
===================================================================
--- django/branches/gis/django/contrib/gis/geos/GEOSGeometry.py 2007-05-31
18:55:16 UTC (rev 5394)
+++ django/branches/gis/django/contrib/gis/geos/GEOSGeometry.py 2007-05-31
23:29:16 UTC (rev 5395)
@@ -114,8 +114,10 @@
# "extern void GEOS_DLL initGEOS(GEOSMessageHandler notice_function,
GEOSMessageHandler error_function);"
lgeos.initGEOS(notice_h, error_h)
-class GEOSGeometry:
+class GEOSGeometry(object):
"A class that, generally, encapsulates a GEOS geometry."
+
+ _g = 0 # Initially NULL
#### Python 'magic' routines ####
def __init__(self, input, geom_type='wkt'):
@@ -123,8 +125,7 @@
if geom_type == 'wkt':
# If the geometry is in WKT form
- buf = create_string_buffer(input)
- g = lgeos.GEOSGeomFromWKT(buf)
+ g = lgeos.GEOSGeomFromWKT(c_char_p(input))
elif geom_type == 'hex':
# If the geometry is in HEX form.
sz = c_size_t(len(input))
@@ -139,7 +140,6 @@
# If the geometry pointer is NULL (0), then raise an exception.
if not g:
- self._g = False # Setting this before raising the exception
raise GEOSException, 'Invalid %s given!' % geom_type.upper()
else:
self._g = g
@@ -251,8 +251,7 @@
the two Geometrys match the elements in pattern."""
if len(pattern) > 9:
raise GEOSException, 'invalid intersection matrix pattern'
- pat = create_string_buffer(pattern)
- return self._predicate(lgeos.GEOSRelatePattern(self._g, other._g, pat))
+ return self._predicate(lgeos.GEOSRelatePattern(self._g, other._g,
c_char_p(pattern)))
def disjoint(self, other):
"Returns true if the DE-9IM intersection matrix for the two Geometrys
is FF*FF****."
@@ -404,7 +403,7 @@
else:
return a.value
-class GEOSCoordSeq:
+class GEOSCoordSeq(object):
"The internal representation of a list of coordinates inside a Geometry."
def __init__(self, ptr, z=False):
@@ -413,13 +412,16 @@
self._z = z
def __del__(self):
+ "Destroys the reference to this coordinate sequence."
if self._cs: lgeos.GEOSCoordSeq_destroy(self._cs)
def __iter__(self):
+ "Iterates over each point in the coordinate sequence."
for i in xrange(self.size):
yield self.__getitem__(i)
def __len__(self):
+ "Returns the number of points in the coordinate sequence."
return int(self.size)
def __str__(self):
@@ -536,6 +538,7 @@
### Other Methods ###
@property
def tuple(self):
+ "Returns a tuple version of this coordinate sequence."
n = self.size
if n == 1:
return self.__getitem__(0)
@@ -587,28 +590,57 @@
"Caches the coordinate sequence."
if not hasattr(self, '_cs'): self._cs = self.coord_seq
+ def __getitem__(self, index):
+ "Gets the point at the specified index."
+ self._cache_cs()
+ if index < 0 or index >= self._cs.size:
+ raise IndexError, 'index out of range'
+ else:
+ return self._cs[index]
+
+ def __iter__(self):
+ "Allows iteration over this LineString."
+ self._cache_cs()
+ for i in xrange(self._cs.size):
+ yield self.__getitem__(index)
+
+ def __len__(self):
+ "Returns the number of points in this LineString."
+ self._cache_cs()
+ return len(self._cs)
+
@property
def tuple(self):
"Returns a tuple version of the geometry from the coordinate sequence."
self._cache_cs()
return self._cs.tuple
-class LinearRing(LineString):
- pass
+# LinearRings are LineStrings used within Polygons.
+class LinearRing(LineString): pass
class Polygon(GEOSGeometry):
- #### Polygon Routines ####
- @property
- def num_interior_rings(self):
- "Returns the number of interior rings."
+ def __getitem__(self, index):
+ """Returns the ring at the specified index. The first index, 0, will
always
+ return the exterior ring. Indices > 0 will return the interior
ring."""
+ if index < 0 or index > self.num_interior_rings:
+ raise IndexError, 'index out of range'
+ else:
+ if index == 0:
+ return self.exterior_ring
+ else:
+ # Getting the interior ring, have to subtract 1 from the index.
+ return self.get_interior_ring(index-1)
- # Getting the number of rings
- n = lgeos.GEOSGetNumInteriorRings(self._g)
+ def __iter__(self):
+ "Iterates over each ring in the polygon."
+ for i in xrange(self.__len__()):
+ yield self.__getitem__(i)
- # -1 indicates an exception occurred
- if n == -1: raise GEOSException, 'Error getting the number of interior
rings!'
- else: return n
+ def __len__(self):
+ "Returns the number of rings in this Polygon."
+ return self.num_interior_rings + 1
+
def get_interior_ring(self, ring_i):
"Gets the interior ring at the specified index."
@@ -616,11 +648,23 @@
# Making sure the ring index is within range
if ring_i >= self.num_interior_rings:
raise GEOSException, 'Invalid ring index.'
-
+
# Getting a clone of the ring geometry at the given ring index.
r = lgeos.GEOSGeom_clone(lgeos.GEOSGetInteriorRingN(self._g,
c_int(ring_i)))
return GEOSGeometry(r, 'geos')
+
+ #### Polygon Properties ####
+ @property
+ def num_interior_rings(self):
+ "Returns the number of interior rings."
+ # Getting the number of rings
+ n = lgeos.GEOSGetNumInteriorRings(self._g)
+
+ # -1 indicates an exception occurred
+ if n == -1: raise GEOSException, 'Error getting the number of interior
rings!'
+ else: return n
+
@property
def exterior_ring(self):
"Gets the exterior ring of the Polygon."
@@ -628,7 +672,17 @@
# Getting a clone of the ring geometry
r = lgeos.GEOSGeom_clone(lgeos.GEOSGetExteriorRing(self._g))
return GEOSGeometry(r, 'geos')
+
+ @property
+ def shell(self):
+ "Gets the shell (exterior ring) of the Polygon."
+ return self.exterior_ring
+ @property
+ def tuple(self):
+ "Gets the tuple for each ring in this Polygon."
+ return tuple(self.__getitem__(i).tuple for i in xrange(self.__len__()))
+
class GeometryCollection(GEOSGeometry):
def _checkindex(self, index):
@@ -648,17 +702,14 @@
return GEOSGeometry(item, 'geos')
def __len__(self):
+ "Returns the number of geometries in this collection."
return self.num_geom
-class MultiPoint(GeometryCollection):
- pass
+# MultiPoint, MultiLineString, and MultiPolygon class definitions.
+class MultiPoint(GeometryCollection): pass
+class MultiLineString(GeometryCollection): pass
+class MultiPolygon(GeometryCollection): pass
-class MultiLineString(GeometryCollection):
- pass
-
-class MultiPolygon(GeometryCollection):
- pass
-
# Class mapping dictionary
GEO_CLASSES = {'Point' : Point,
'Polygon' : Polygon,
Modified: django/branches/gis/django/contrib/gis/tests/geometries.py
===================================================================
--- django/branches/gis/django/contrib/gis/tests/geometries.py 2007-05-31
18:55:16 UTC (rev 5394)
+++ django/branches/gis/django/contrib/gis/tests/geometries.py 2007-05-31
23:29:16 UTC (rev 5395)
@@ -52,11 +52,14 @@
)
# Polygons
-polygons = (TestGeom('POLYGON ((0 0, 0 100, 100 100, 100 0, 0 0), (10 10, 10
90, 90 90, 90 10, 10 10) ))',
- n_i=1, ext_ring_cs=((0, 0), (0, 100), (100, 100), (100,
0), (0, 0))
+polygons = (TestGeom('POLYGON ((0 0, 0 100, 100 100, 100 0, 0 0), (10 10, 10
90, 90 90, 90 10, 10 10))',
+ n_i=1, ext_ring_cs=((0, 0), (0, 100), (100, 100), (100,
0), (0, 0)), pc=10,
),
+ TestGeom('POLYGON ((0 0, 0 100, 100 100, 100 0, 0 0), (10 10, 10
20, 20 20, 20 10, 10 10), (80 80, 80 90, 90 90, 90 80, 80 80))',
+ n_i=2, ext_ring_cs=((0, 0), (0, 100), (100, 100), (100,
0), (0, 0)), pc=15,
+ ),
TestGeom('POLYGON ((0 0, 0 100, 100 100, 100 0, 0 0))',
- n_i=0, ext_ring_cs=((0, 0), (0, 100), (100, 100), (100,
0), (0, 0))
+ n_i=0, ext_ring_cs=((0, 0), (0, 100), (100, 100), (100,
0), (0, 0)), pc=10,
),
TestGeom('POLYGON ((-95.3848703124799471 29.7056021479768511,
-95.3851905195191847 29.7046588196500281, -95.3859356966379011
29.7025053545605502, -95.3860723000647539 29.7020963367038391,
-95.3871517697222089 29.6989779021280995, -95.3865578518265522
29.6990856888057202, -95.3862634205175226 29.6999471753441782,
-95.3861991779541967 29.6999591988978615, -95.3856773799358137
29.6998323107113578, -95.3856209915427229 29.6998005235473741,
-95.3855833545501639 29.6996619391729801, -95.3855776331865002
29.6996232659570047, -95.3850162731712885 29.6997236706530536,
-95.3831047357410284 29.7000847603095082, -95.3829800724914776
29.7000676365023502, -95.3828084594470909 29.6999969684031200,
-95.3828131504821499 29.6999090511531065, -95.3828022942979601
29.6998152117366025, -95.3827893930918833 29.6997790953076759,
-95.3825174668099862 29.6998267772748825, -95.3823521544804862
29.7000451723151606, -95.3820491918785223 29.6999682034582335,
-95.3817932841505893 29.69996404!
07204772, -95.3815438924600443 29.7005983712500630, -95.3807812390843424
29.7007538492921590, -95.3778578936435935 29.7012966201172048,
-95.3770817300034679 29.7010555145969093, -95.3772763716395957
29.7004995005932031, -95.3769891024414420 29.7005797730360186,
-95.3759855007185990 29.7007754783987821, -95.3759516423090474
29.7007305400669388, -95.3765252155960042 29.6989549173240874,
-95.3766842746727832 29.6985134987163164, -95.3768510987262914
29.6980530300744938, -95.3769198676258014 29.6977137204527573,
-95.3769616670751930 29.6973351617272172, -95.3770309229297766
29.6969821084304186, -95.3772352596880637 29.6959751305871613,
-95.3776232419333354 29.6945439060847463, -95.3776849628727064
29.6943364710766069, -95.3779699491714723 29.6926548349458947,
-95.3781945479573494 29.6920088336742545, -95.3785807118394189
29.6908279316076005, -95.3787441368896651 29.6908846275832197,
-95.3787903214163890 29.6907152912461640, -95.3791765069353659
29.6893335376821526, -95.37949359!
59513026 29.6884781789101595, -95.3796592071232112 29.68800666!
81407619
, -95.3799788182090111 29.6873687353035081, -95.3801545516183893
29.6868782380716993, -95.3801258908302145 29.6867756621337762,
-95.3801104284899566 29.6867229678809572, -95.3803803523746154
29.6863753372986459, -95.3821028558287622 29.6837392961470421,
-95.3827289584682205 29.6828097375216160, -95.3827494698109035
29.6790739156259278, -95.3826022014838486 29.6776502228345507,
-95.3825047356438063 29.6765773006280753, -95.3823473035336917
29.6750405250369127, -95.3824540163482055 29.6750076408228587,
-95.3838984230304305 29.6745679207378679, -95.3916547074937426
29.6722459226508377, -95.3926154662749468 29.6719609085105489,
-95.3967246645118081 29.6707316485589736, -95.3974588054406780
29.6705065336410989, -95.3978523748756828 29.6703795547846845,
-95.3988598162279970 29.6700874981900853, -95.3995628600665952
29.6698505300412414, -95.4134721665944170 29.6656841279906232,
-95.4143262068232616 29.6654291174019278, -95.4159685142480214
29.6649750989232288, -95.4180067396277565 !
29.6643253024318021, -95.4185886692196590 29.6641482768691063,
-95.4234155309609662 29.6626925393704788, -95.4287785503196346
29.6611023620959706, -95.4310287312749352 29.6604222580752648,
-95.4320295629628959 29.6603361318136720, -95.4332899683975739
29.6600560661713608, -95.4342675748811047 29.6598454934599900,
-95.4343110414310871 29.6598411486215490, -95.4345576779282538
29.6598147020668499, -95.4348823041721630 29.6597875803673112,
-95.4352827715209457 29.6597762346946681, -95.4355290431309982
29.6597827926562374, -95.4359197997999331 29.6598014511782715,
-95.4361907884752156 29.6598444333523368, -95.4364608955807228
29.6598901433108217, -95.4367250147512323 29.6599494499910712,
-95.4364898759758091 29.6601880616540186, -95.4354501111810691
29.6616378572201107, -95.4381459623171224 29.6631265631655126,
-95.4367852490863129 29.6642266600024023, -95.4370040894557263
29.6643425389568769, -95.4367078350812648 29.6645492592343238,
-95.4366081749871285 29.6646291473027297, -!
95.4358539359938192 29.6652308742342932, -95.4350327668927889 !
29.66589
95989314462, -95.4350580905272921 29.6678812477895271, -95.4349710541447536
29.6680054925936965, -95.4349500440473548 29.6671410080890006,
-95.4341492724148850 29.6678790545191688, -95.4340248868274728
29.6680353198492135, -95.4333227845797438 29.6689245624945990,
-95.4331325652123326 29.6691616138940901, -95.4321314741096955
29.6704473333237253, -95.4320435792664341 29.6702578985411982,
-95.4320147929883547 29.6701800936425109, -95.4319764538662980
29.6683246590817085, -95.4317490976340679 29.6684974372577166,
-95.4305958185342718 29.6694049049170374, -95.4296600735653016
29.6701723430938493, -95.4284928989940937 29.6710931793380972,
-95.4274630532378580 29.6719378813640091, -95.4273056811974811
29.6720684984625791, -95.4260554084574864 29.6730668861566969,
-95.4253558063699643 29.6736342467365724, -95.4249278826026028
29.6739557343648919, -95.4248648873821423 29.6745400910786152,
-95.4260016131471929 29.6750987014005858, -95.4258567183010911
29.6753452063069929, -95.426023!
8081486847 29.6754322077221353, -95.4258707374502393 29.6756647377294307,
-95.4257951755816691 29.6756407098663360, -95.4257701599566985
29.6761077719536068, -95.4257726684792260 29.6761711204603955,
-95.4257980187195614 29.6770219651929423, -95.4252712669032519
29.6770161558853758, -95.4249234392992065 29.6770068683962300,
-95.4249574272905789 29.6779707498635759, -95.4244725881033702
29.6779825646764159, -95.4222269476429545 29.6780711474441716,
-95.4223032371999267 29.6796029391538809, -95.4239133706588945
29.6795331493690355, -95.4224579084327331 29.6813706893847780,
-95.4224290108823965 29.6821953228763924, -95.4230916478977349
29.6822130268724109, -95.4222928279595521 29.6832041816675343,
-95.4228763710016352 29.6832087677714505, -95.4223401691637179
29.6838987872753748, -95.4211655906087088 29.6838784024852984,
-95.4201984153205558 29.6851319258758082, -95.4206156387716362
29.6851623398125319, -95.4213438084897660 29.6851763011334739,
-95.4212071118618752 29.68536799!
31624974, -95.4202651399651245 29.6865313962980508, -95.417206!
11576597
83 29.6865816431043932, -95.4182217951255183 29.6872251197301544,
-95.4178664826439160 29.6876750901471631, -95.4180678442928780
29.6877960336377207, -95.4188763472917572 29.6882826379510938,
-95.4185374500596311 29.6887137897831934, -95.4182121713132290
29.6885097429738813, -95.4179857231741551 29.6888118367840086,
-95.4183106010563620 29.6890048676118212, -95.4179489865331334
29.6894546700979056, -95.4175581746284820 29.6892323606815438,
-95.4173439957341571 29.6894990139807007, -95.4177411199311081
29.6897435034738422, -95.4175789200209721 29.6899207529979208,
-95.4170598559864800 29.6896042165807508, -95.4166733682539814
29.6900891174451367, -95.4165941362704331 29.6900347214235047,
-95.4163537218065301 29.6903529467753238, -95.4126843270708775
29.6881086357212780, -95.4126604121378392 29.6880942378803496,
-95.4126672298953338 29.6885951670109982, -95.4126680884821923
29.6887052446594275, -95.4158080137241882 29.6906382377959339,
-95.4152061403821961 29.6910871045531586,!
-95.4155842583188161 29.6917382915894308, -95.4157426793520358
29.6920726941677096, -95.4154520563662203 29.6922052332446427,
-95.4151389936167078 29.6923261661269571, -95.4148649784384872
29.6924343866430256, -95.4144051352401590 29.6925623927348106,
-95.4146792019416665 29.6926770338507744, -95.4148824479948985
29.6928117893696388, -95.4149851734360226 29.6929823719519774,
-95.4140436551925291 29.6929626643100946, -95.4140465993023241
29.6926545917254892, -95.4137269186733334 29.6927395764256090,
-95.4137372859685513 29.6935432485666624, -95.4135702836218655
29.6933186678088283, -95.4133925235973237 29.6930415229852152,
-95.4133017035615580 29.6928685062036166, -95.4129588921634593
29.6929391128977862, -95.4125107395559695 29.6930481664661485,
-95.4102647423187307 29.6935850183258019, -95.4081931340840157
29.6940907430947760, -95.4078783596459772 29.6941703429951609,
-95.4049213975000043 29.6948723732981961, -95.4045944244127071
29.6949626434239207, -95.4045865139788134 !
29.6954109019001358, -95.4045953345484037 29.6956972800496963,!
-95.403
8879332535146 29.6958296089365490, -95.4040366394459340 29.6964389004769842,
-95.4032774779020798 29.6965643341263892, -95.4026066501239853
29.6966646227683881, -95.4024991226393837 29.6961389766619703,
-95.4011781398631911 29.6963566063186377, -95.4011524097636112
29.6962596176762190, -95.4018184046368276 29.6961399466727336,
-95.4016995838361908 29.6956442609415099, -95.4007100753964608
29.6958900524002978, -95.4008032469935188 29.6962639900781404,
-95.3995660267125487 29.6965636449370329, -95.3996140564775601
29.6967877962763644, -95.3996364430014410 29.6968901984825280,
-95.3984003269631842 29.6968679634805746, -95.3981442026887265
29.6983660679730335, -95.3980178461957706 29.6990890276252415,
-95.3977097967130163 29.7008526152273049, -95.3962347157626027
29.7009697553607630, -95.3951949050136250 29.7004740386619019,
-95.3957564950617183 29.6990281830553187, -95.3965927101519924
29.6968771129030706, -95.3957496517238184 29.6970800358387095,
-95.3957720559467361 29.697226!
4611230727, -95.3957391586571788 29.6973548894558732, -95.3956286413405365
29.6974949857280883, -95.3955111053256957 29.6975661086270186,
-95.3953215342724121 29.6976022763384790, -95.3951795558443365
29.6975846977491038, -95.3950369632041060 29.6975175779330200,
-95.3949401089966500 29.6974269267953304, -95.3948740281415581
29.6972903308506346, -95.3946650813866910 29.6973397326847923,
-95.3947654059391112 29.6974882560192022, -95.3949627316619768
29.6980355864961858, -95.3933200807862249 29.6984590863712796,
-95.3932606497523494 29.6984464798710839, -95.3932983699113350
29.6983154306484352, -95.3933058014696655 29.6982165816983610,
-95.3932946347785133 29.6981089778195759, -95.3931780601756287
29.6977068906794841, -95.3929928222970602 29.6977541771878180,
-95.3930873169846478 29.6980676264932946, -95.3932743746374570
29.6981249406449663, -95.3929512584706316 29.6989526513922222,
-95.3919850280655197 29.7014358632108646, -95.3918950918929056
29.7014169320765724, -95.391692!
8317890296 29.7019232352846423, -95.3915424614970959 29.702298!
87129282
89, -95.3901530441668939 29.7058519502930061, -95.3899656322116698
29.7059156823562418, -95.3897628748670883 29.7059900058266777,
-95.3896062677805787 29.7060738276384946, -95.3893941800512266
29.7061891695242046, -95.3892150365492455 29.7062641292949436,
-95.3890502563035199 29.7063339729630940, -95.3888717930715586
29.7063896908080736, -95.3886925428988945 29.7064453871994978,
-95.3885376849411983 29.7064797304524149, -95.3883284158984139
29.7065153575050189, -95.3881046767627794 29.7065368368267357,
-95.3878809284696132 29.7065363048447537, -95.3876046356120924
29.7065288525102424, -95.3873060894974714 29.7064822806001452,
-95.3869851943158409 29.7063993367575350, -95.3865967896568065
29.7062870572919202, -95.3861785624983156 29.7061492099008184,
-95.3857375009733488 29.7059887337478798, -95.3854573290902152
29.7058683664514618, -95.3848703124799471 29.7056021479768511))',
n_i=0, ext_ring_cs=False
Modified: django/branches/gis/django/contrib/gis/tests/test_geos.py
===================================================================
--- django/branches/gis/django/contrib/gis/tests/test_geos.py 2007-05-31
18:55:16 UTC (rev 5394)
+++ django/branches/gis/django/contrib/gis/tests/test_geos.py 2007-05-31
23:29:16 UTC (rev 5395)
@@ -82,6 +82,7 @@
self.assertEqual(poly.empty, False)
self.assertEqual(poly.ring, False)
self.assertEqual(p.n_i, poly.num_interior_rings)
+ self.assertEqual(p.n_i + 1, len(poly)) # Testing __len__
# Testing the geometry equivalence
self.assertEqual(True, poly == GEOSGeometry(p.wkt))
@@ -94,6 +95,7 @@
self.assertEqual(ring.geom_typeid, 2)
if p.ext_ring_cs:
self.assertEqual(p.ext_ring_cs, ring.tuple)
+ self.assertEqual(p.ext_ring_cs, poly[0].tuple) # Testing
__getitem__
def test03_multipolygons(self):
"Testing MultiPolygon objects."
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---