changeset 2:82c4d16dc9d6

Adding comparison and better postgis support
author Nicolas Évrard <nicoe@b2ck.com>
date Tue, 31 Jan 2012 17:37:25 +0100
parents c44983f8a17d
children 83818224c332
files equality_comp modular_dbapi series
diffstat 3 files changed, 109 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/equality_comp	Tue Jan 31 17:37:25 2012 +0100
@@ -0,0 +1,88 @@
+# HG changeset patch
+# Parent 84e382eafd073fea9de97e14a5652b5d7e871f61
+diff -r 84e382eafd07 -r 6f65b5bb65e0 src/ppygis.py
+--- a/src/ppygis.py	Fri Dec 23 12:44:52 2011 +0100
++++ b/src/ppygis.py	Tue Jan 31 17:34:41 2012 +0100
+@@ -31,6 +31,7 @@
+ import binascii
+ import cStringIO
+ import struct
++from itertools import izip_longest
+ 
+ 
+ class _EWKBReader(object):
+@@ -267,11 +268,15 @@
+         return cls(reader.read_double(), reader.read_double(), srid=srid)
+ 
+     def __eq__(self, other):
+-        return (self.srid == other.srid
+-            and self.x == other.x
+-            and self.y == other.y
+-            and self.z == other.z
+-            and self.m == other.m)
++        if not isinstance(other, Point):
++            return False
++        for attribute in ('srid', 'x', 'y', 'z', 'm'):
++            if getattr(self, attribute) != getattr(other, attribute):
++                return False
++        return True
++
++    def __ne__(self, other):
++        return not (self == other)
+ 
+     def __str__(self):
+         return 'Point(X: {0}, Y: {1}'.format(self.x, self.y) + \
+@@ -314,6 +319,17 @@
+             else:
+                 raise Exception('invalid geometry')
+ 
++    def __eq__(self, other):
++        if not isinstance(other, LineString):
++            return False
++        for point_self, point_other in izip_longest(self.points, other.points):
++            if point_self != point_other:
++                return False
++        return True
++
++    def __ne__(self, other):
++        return not (self == other)
++
+     def __str__(self):
+         return 'LineString(' + ', '.join([str(point) for point in
+                 self.points]) + self._str_srid() + ')'
+@@ -352,6 +368,17 @@
+             else:
+                 raise Exception('invalid geometry')
+ 
++    def __eq__(self, other):
++        if not isinstance(other, Polygon):
++            return False
++        for ring_self, ring_other in izip_longest(self.rings, other.rings):
++            if ring_self != ring_other:
++                return False
++        return True
++
++    def __ne__(self, other):
++        return not (self == other)
++
+     def __str__(self):
+         return 'Polygon(' + ', '.join([str(ring) for ring in self.rings]) + \
+                 self._str_srid() + ')'
+@@ -383,6 +410,17 @@
+         for geometry in self.geometries:
+             geometry._write_ewkb(writer)
+ 
++    def __eq__(self, other):
++        if not isinstance(other, type(self)):
++            return False
++        for geo1, geo2 in izip_longest(self.geometries, other.geometries):
++            if geo1 != geo2:
++                return False
++        return True
++
++    def __ne__(self, other):
++        return not (self == other)
++
+     def __str__(self):
+         return 'GeometryCollection(' + ', '.join([str(geometry) for geometry in
+                 self.geometries]) + self._str_srid() + ')'
--- a/modular_dbapi	Fri Dec 23 12:32:51 2011 +0100
+++ b/modular_dbapi	Tue Jan 31 17:37:25 2012 +0100
@@ -3,7 +3,7 @@
 
 diff -r a29eb0d4a386 src/ppygis.py
 --- a/src/ppygis.py	Sun Dec 04 15:32:39 2011 +0100
-+++ b/src/ppygis.py	Thu Dec 22 17:30:30 2011 +0100
++++ b/src/ppygis.py	Tue Jan 31 17:36:44 2012 +0100
 @@ -32,8 +32,6 @@
  import cStringIO
  import struct
@@ -132,7 +132,17 @@
      def __str__(self):
          return 'Point(X: {0}, Y: {1}'.format(self.x, self.y) + \
                  (', Z: {0}'.format(self.z) if self.has_z else '') + \
-@@ -251,8 +281,7 @@
+@@ -218,8 +248,7 @@
+ 
+     def __init__(self, points, srid=None):
+         self.points = list(points)
+-        if srid:
+-            self.srid = srid
++        self.srid = srid
+ 
+     @property
+     def has_z(self):
+@@ -251,8 +280,7 @@
  
      def __init__(self, rings, srid=None):
          self.rings = list(rings)
@@ -142,7 +152,7 @@
  
      @property
      def has_z(self):
-@@ -284,8 +313,7 @@
+@@ -284,8 +312,7 @@
  
      def __init__(self, geometries, srid=None):
          self.geometries = list(geometries)
@@ -152,7 +162,7 @@
  
      @property
      def has_z(self):
-@@ -314,8 +342,7 @@
+@@ -314,8 +341,7 @@
  
      def __init__(self, points, srid=None):
          self.points = list(points)
@@ -162,7 +172,7 @@
  
      @property
      def has_z(self):
-@@ -352,8 +379,7 @@
+@@ -352,8 +378,7 @@
  
      def __init__(self, lines, srid=None):
          self.lines = list(lines)
@@ -172,7 +182,7 @@
  
      @property
      def has_z(self):
-@@ -390,8 +416,7 @@
+@@ -390,8 +415,7 @@
  
      def __init__(self, polygons, srid=None):
          self.polygons = list(polygons)
@@ -182,7 +192,7 @@
  
      @property
      def has_z(self):
-@@ -422,3 +447,15 @@
+@@ -422,3 +446,17 @@
      def __str__(self):
          return 'MultiPolygon(' + ', '.join([str(polygon) for polygon in
                  self.polygons]) + self._str_srid() + ')'
@@ -193,7 +203,9 @@
 +
 +    GEOMETRY = new_type((436421,), "POINT", Geometry.read_ewkb)
 +    register_type(GEOMETRY)
-+    register_adapter(Point, lambda o: AsIs("'%s'" % o.write_ewkb()))
++    for klass in (Point, LineString, Polygon, MultiPoint, MultiLineString,
++            MultiPolygon, GeometryCollection):
++        register_adapter(klass, lambda o: AsIs("'%s'" % o.write_ewkb()))
 +
 +def register_spatialite():
 +    from pyspatialite.dbapi2 import register_converter
--- a/series	Fri Dec 23 12:32:51 2011 +0100
+++ b/series	Tue Jan 31 17:37:25 2012 +0100
@@ -1,3 +1,4 @@
 modular_dbapi
 unittests
 add_wkt
+equality_comp