Base class for Shapes?
Jan-Oliver Wagner
jan at intevation.de
Wed Nov 24 22:49:26 CET 2004
Hi,
on a train travel from Bonn to Osnabrück
I worked out an idea about a Base Shape Class.
See the attached patch.
The idea is that we might want to have Shapes/Shapestores
that are kept in memory instead in a file in order
have a opportunity to create/modify them with arbitrary
algorithms implemented in Thuban(or in its extensions).
The idea is taken from a large Thuban Extension on precision
farming developed by Ole Rahn.
This class "Shape" would also be a first step to redruce
the code in that extension. Ongoing steps would be to introduce
a MemoryShapeStore with ability to store it as a Shapefile.
What I want to know is whether this idea is a sensible way to
go or not. Any feedback welcome.
Ah, and: this can even be ported back to 1_0 ;-)
Best
Jan
--
Jan-Oliver Wagner http://intevation.de/~jan/
Intevation GmbH http://intevation.de/
FreeGIS http://freegis.org/
-------------- next part --------------
Index: Thuban/Model/data.py
===================================================================
RCS file: /home/thuban/jail/thubanrepository/thuban/Thuban/Model/data.py,v
retrieving revision 1.15
diff -u -3 -p -r1.15 data.py
--- Thuban/Model/data.py 24 Nov 2003 19:23:08 -0000 1.15
+++ Thuban/Model/data.py 24 Nov 2004 21:43:02 -0000
@@ -48,8 +48,114 @@ RAW_SHAPEFILE = "RAW_SHAPEFILE"
# Raw data in well-known text format
RAW_WKT = "RAW_WKT"
+class Shape:
-class ShapefileShape:
+ """Base class (Interface) for representing a single Shape.
+ """
+
+ def __init__(self):
+ """Initialize this shape.
+ """
+ self.shapeid = None
+ self.bbox = None
+ self.points = []
+
+ def ShapeID(self):
+ """Return the unique ID of this shape or None if there is none
+ available.
+ """
+ return self.shapeid
+
+ def setShapeID(self, id):
+ """Set the ID of this shape.
+
+ id -- The unique ID of the shape or None if it hasn't one.
+ """
+ self.shapeid = id
+
+ def BBox(self):
+ """Return the bounding box of this shape.
+
+ If it has yet not been computed, this will be done
+ now and the bbox be stored for later use without
+ computation.
+
+ Returns: Bounding box as tuple (minx, miny, maxx, maxy).
+ """
+ if self.bbox is None:
+ xs = []
+ ys = []
+ for part in self.Points():
+ for x, y in part:
+ xs.append(x)
+ ys.append(y)
+ if len(xs): # there must be at least one vertix
+ self.bbox = (min(xs), min(ys), max(xs), max(ys))
+ return self.bbox
+
+ def Points(self):
+ """Return the coordinates of the shape as a list (parts)
+ of lists (sequences) of coordinate pairs.
+ E.g.: [[(x0,y0),(x1,y1),(x2,y2)]]
+
+ Classes inheriting from Shape should overwrite this
+ method.
+ """
+ return self.points
+
+ def setPoints(self, points):
+ """Set the points for this shape. Also, the bounding box is
+ reset and will be computed anew when used next time.
+
+ points -- a list (parts) of a list (sequences) of coordinate
+ pairs, e.g. [ [ (x0,y0),(x1,y1) ] ]
+ """
+ self.points = points
+ self.bbox = None
+
+ def isInBBox(self, bbox):
+ """Test whether the the shape lies completely within the
+ given bounding box.
+
+ bbox -- the bounding box as tuple (minx,miny,max,maxy)
+
+ Returns: True if the shape lies within bbox, else False
+ """
+ bb_minX, bb_minY, bb_maxX, bb_maxY = bbox
+ minX, minY, maxX, maxY = self.BBox()
+
+ if minX >= bb_minX and maxX <= bb_maxX and \
+ minY >= bb_minY and maxY <= bb_maxY:
+ return True
+
+ return False
+
+ def __str__(self):
+ return "Shape " + repr(self.shapeid) + ": " + repr(self.points)
+
+# def intersectsBBox(self, bbox):
+# """Test whether the the shape intersects the given bounding box.
+# In other words, tests whether at least one point of the shape
+# lies within the given bounding box.
+#
+# bbox -- the bounding box as tuple (minx,miny,max,maxy)
+#
+# Returns: True if at least one point of the shape lies within
+# bbox, else False
+# """
+# bb_minX, bb_minY, bb_maxX, bb_maxY = bbox
+# minX, minY, maxX, maxY = self.BBox()
+#
+# # test each single point whether it is inside the given bbox
+# for part in self.Points():
+# for x, y in part:
+# if (x >= bb_minX and x <= bb_maxX) and
+# (y >= bb_minY and y <= bb_maxY):
+# return True
+#
+# return False
+
+class ShapefileShape(Shape):
"""Represent one shape of a shapefile"""
@@ -60,17 +166,13 @@ class ShapefileShape:
def compute_bbox(self):
"""
Return the bounding box of the shape as a tuple (minx,miny,maxx,maxy)
- """
- xs = []
- ys = []
- for part in self.Points():
- for x, y in part:
- xs.append(x)
- ys.append(y)
- return (min(xs), min(ys), max(xs), max(ys))
- def ShapeID(self):
- return self.shapeid
+ This method is deprecated since 2004-11-23.
+ Use BBox() instead.
+ """
+ print "compute_bbox():This method is deprecated since 2004-11-23."
+ print "Use BBox() instead."
+ return self.BBox()
def Points(self):
"""Return the coordinates of the shape as a list of lists of pairs"""
@@ -81,7 +183,9 @@ class ShapefileShape:
return points
def RawData(self):
- """Return the shape id to use with the shapefile"""
+ """Return the shape id to use with the shapefile.
+
+ XXX: What is the difference to ShapeID()?"""
return self.shapeid
def Shapefile(self):
More information about the Thuban-devel
mailing list
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)