gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / geom_2_3_0.py @ 631
History | View | Annotate | Download (9.85 KB)
1 |
# -*- coding: utf-8 -*-
|
---|---|
2 |
#
|
3 |
# File: geom.py
|
4 |
#
|
5 |
# Copyright (c) 2011 by Model Driven Development sl and Antonio Carrasco Valero
|
6 |
#
|
7 |
# GNU General Public License (GPL)
|
8 |
#
|
9 |
# This program is free software; you can redistribute it and/or
|
10 |
# modify it under the terms of the GNU General Public License
|
11 |
# as published by the Free Software Foundation; either version 2
|
12 |
# of the License, or (at your option) any later version.
|
13 |
#
|
14 |
# This program is distributed in the hope that it will be useful,
|
15 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17 |
# GNU General Public License for more details.
|
18 |
#
|
19 |
# You should have received a copy of the GNU General Public License
|
20 |
# along with this program; if not, write to the Free Software
|
21 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
22 |
# 02110-1301, USA.
|
23 |
#
|
24 |
#
|
25 |
|
26 |
"""
|
27 |
Utility functions to manage gvSIG geometries
|
28 |
"""
|
29 |
|
30 |
__author__ = """Antonio Carrasco Valero
|
31 |
Model Driven Development sl and Antonio Carrasco Valero
|
32 |
<carrasco@modeldd.org>
|
33 |
Victor Acevedo Royer <vacevedo@gvsig.com> <vacevedor@gmail.com>
|
34 |
"""
|
35 |
|
36 |
__docformat__ = 'plaintext'
|
37 |
|
38 |
|
39 |
from org.gvsig.fmap.geom import Geometry, GeometryLocator |
40 |
from org.gvsig.tools import ToolsLocator |
41 |
from org.gvsig.fmap.geom import Geometry |
42 |
from org.gvsig.fmap.geom.primitive import Line, Polygon, Point |
43 |
|
44 |
#GeometryTypes
|
45 |
AGGREGATE = Geometry.TYPES.AGGREGATE |
46 |
ARC = Geometry.TYPES.ARC |
47 |
CIRCLE = Geometry.TYPES.CIRCLE |
48 |
CURVE = Geometry.TYPES.CURVE |
49 |
ELLIPSE = Geometry.TYPES.ELLIPSE |
50 |
ELLIPTICARC = Geometry.TYPES.ELLIPTICARC |
51 |
GEOMETRY = Geometry.TYPES.GEOMETRY |
52 |
MULTICURVE = Geometry.TYPES.MULTICURVE |
53 |
MULTIPOINT = Geometry.TYPES.MULTIPOINT |
54 |
MULTISOLID = Geometry.TYPES.MULTISOLID |
55 |
MULTISURFACE = Geometry.TYPES.MULTISURFACE |
56 |
NULL = Geometry.TYPES.NULL |
57 |
POINT = Geometry.TYPES.POINT |
58 |
SOLID = Geometry.TYPES.SOLID |
59 |
SPLINE = Geometry.TYPES.SPLINE |
60 |
SURFACE = Geometry.TYPES.SURFACE |
61 |
|
62 |
# Common named geometry types
|
63 |
POLYGON = Geometry.TYPES.SURFACE |
64 |
LINE = Geometry.TYPES.CURVE |
65 |
MULTILINE = Geometry.TYPES.MULTICURVE |
66 |
MULTIPOLYGON = Geometry.TYPES.MULTISURFACE |
67 |
|
68 |
# geometrySubTypes
|
69 |
D2 = Geometry.SUBTYPES.GEOM2D |
70 |
D2M = Geometry.SUBTYPES.GEOM2DM |
71 |
D3 = Geometry.SUBTYPES.GEOM3D |
72 |
D3M = Geometry.SUBTYPES.GEOM3DM |
73 |
UNKNOWN = Geometry.SUBTYPES.UNKNOWN |
74 |
|
75 |
# Dimensions
|
76 |
DIMENSIONS = Geometry.DIMENSIONS |
77 |
|
78 |
def createGeometry(type, subtype=D2): |
79 |
"""
|
80 |
Returns a new geometry with a concrete type and subtype or None if can't
|
81 |
create geometry.
|
82 |
:param type: geometry type
|
83 |
:type type: string
|
84 |
:param subtype: geometry subtype
|
85 |
:type type: string
|
86 |
:return: geometry
|
87 |
:rtype: geometry
|
88 |
"""
|
89 |
try:
|
90 |
geometryManager = GeometryLocator.getGeometryManager() |
91 |
geometry = geometryManager.create(type, subtype)
|
92 |
except:
|
93 |
return None |
94 |
return geometry
|
95 |
|
96 |
def createPoint2D(x=None, y=None): |
97 |
if x==None and y==None: |
98 |
return createPoint(D2)
|
99 |
elif x==None or y==None: |
100 |
return None |
101 |
else:
|
102 |
return createPoint(D2, x, y)
|
103 |
|
104 |
def createLine2D(vertexes=None): |
105 |
if vertexes==None: |
106 |
return createLine(D2)
|
107 |
else:
|
108 |
return createLine(D2, vertexes)
|
109 |
|
110 |
def createPolygon2D(vertexes=None): |
111 |
if vertexes==None: |
112 |
return createPolygon(D2)
|
113 |
else:
|
114 |
return createPolygon(D2, vertexes)
|
115 |
|
116 |
|
117 |
def createPoint(subtype=D2,*coords): |
118 |
"""
|
119 |
Returns a new point with a subtype and sets the value for the X, Y, Z and M
|
120 |
coordinates (default 0,0,0,0) or None if can't create point
|
121 |
:param subtype: geometry type
|
122 |
:type subtype: integer
|
123 |
:param coords: list of coordinates
|
124 |
:type coords: list
|
125 |
:return: point
|
126 |
:rtype: Geometry
|
127 |
"""
|
128 |
|
129 |
geometryManager = GeometryLocator.getGeometryManager() |
130 |
point = geometryManager.create(POINT, subtype) |
131 |
|
132 |
try:
|
133 |
#If is geometry, transform to a new point type.
|
134 |
if len(coords)==0: |
135 |
return point
|
136 |
else:
|
137 |
# Coords with one parameter
|
138 |
# Check if is a Geometry
|
139 |
|
140 |
if len(coords)==1: #FIXME |
141 |
coords = coords[0]
|
142 |
|
143 |
if isinstance(coords, Point): |
144 |
return coords
|
145 |
|
146 |
#Check if object have GET attribute
|
147 |
get = getattr(coords,"__getitem__",None) |
148 |
if get != None and len(coords)>=2: |
149 |
if subtype==D2:
|
150 |
point.setCoordinateAt(Geometry.DIMENSIONS.X, get(0))
|
151 |
point.setCoordinateAt(Geometry.DIMENSIONS.Y, get(1))
|
152 |
elif subtype==D3:
|
153 |
point.setCoordinateAt(Geometry.DIMENSIONS.X, get(0))
|
154 |
point.setCoordinateAt(Geometry.DIMENSIONS.Y, get(1))
|
155 |
try:
|
156 |
z = get(2)
|
157 |
except:
|
158 |
z = 0
|
159 |
point.setCoordinateAt(Geometry.DIMENSIONS.Z, z) |
160 |
elif subtype==D2M:
|
161 |
point.setCoordinateAt(Geometry.DIMENSIONS.X, get(0))
|
162 |
point.setCoordinateAt(Geometry.DIMENSIONS.Y, get(1))
|
163 |
try:
|
164 |
m = get(2)
|
165 |
except:
|
166 |
m = 0
|
167 |
point.setCoordinateAt(point.getDimension()-1, m)
|
168 |
elif subtype==D3M:
|
169 |
point.setCoordinateAt(Geometry.DIMENSIONS.X, get(0))
|
170 |
point.setCoordinateAt(Geometry.DIMENSIONS.Y, get(1))
|
171 |
try:
|
172 |
z = get(2)
|
173 |
except:
|
174 |
z = 0
|
175 |
point.setCoordinateAt(Geometry.DIMENSIONS.Z, z) |
176 |
try:
|
177 |
m = get(3)
|
178 |
except:
|
179 |
m = 0
|
180 |
point.setCoordinateAt(point.getDimension()-1, m)
|
181 |
return point
|
182 |
except:
|
183 |
return None |
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
def createMultiPoint(subtype=D2, points=None): |
190 |
"""
|
191 |
Returns a new multipoint with a subtype from a list of Points instances.
|
192 |
Also values of X and Y tuples like ([x1, y1], [x2, y2], ...., [xn, yn]) are
|
193 |
allowed. If not points returns empty multipoint geometry. If can't create
|
194 |
geometry return None.
|
195 |
:param subtype: geometry type
|
196 |
:type subtype: integer
|
197 |
:param points: list of points
|
198 |
:type points: list
|
199 |
:return: multipoint
|
200 |
:rtype: multipoint
|
201 |
"""
|
202 |
|
203 |
multipoint = createGeometry(MULTIPOINT, subtype) |
204 |
if points!=None: |
205 |
try:
|
206 |
for point in points: |
207 |
if isinstance(point, Geometry): |
208 |
pgeom = point |
209 |
else:
|
210 |
pgeom = createPoint(subtype, point) |
211 |
multipoint.addPrimitive(pgeom) |
212 |
except:
|
213 |
return None |
214 |
|
215 |
return multipoint
|
216 |
|
217 |
def createPolygon(subtype=D2, vertexes=None): |
218 |
"""
|
219 |
Returns a new polygon with a subtype. Or None if can't create the geometry
|
220 |
:return: polygon
|
221 |
:rtype: Geometry
|
222 |
"""
|
223 |
polygon = createGeometry(SURFACE, subtype) |
224 |
if vertexes!= None: |
225 |
try:
|
226 |
for vertex in vertexes: |
227 |
vgeom = createPoint(subtype, vertex) |
228 |
polygon.addVertex(vgeom) |
229 |
|
230 |
except:
|
231 |
return None |
232 |
return polygon
|
233 |
|
234 |
def createMultiPolygon(subtype=D2, polygons=None): |
235 |
"""
|
236 |
Returns a new multipolygon with a subtype. or None if can't create geometry
|
237 |
:return: multipolygon
|
238 |
:rtype: Geometry
|
239 |
"""
|
240 |
multipolygon = createGeometry(MULTISURFACE, subtype) |
241 |
if polygons!=None: |
242 |
try:
|
243 |
for polygon in polygons: |
244 |
if isinstance(polygon, Polygon): #, Polygon) |
245 |
pass
|
246 |
else:
|
247 |
polygon = createPolygon(subtype=subtype,vertexes=polygon) |
248 |
multipolygon.addPrimitive(polygon) |
249 |
except:
|
250 |
return None |
251 |
|
252 |
return multipolygon
|
253 |
|
254 |
def createLine(subtype=D2, vertexes=None): |
255 |
"""
|
256 |
Returns a new line with a subtype or None if can't create geometry
|
257 |
:return: polygon
|
258 |
:rtype: Geometry
|
259 |
"""
|
260 |
line = createGeometry(CURVE, subtype) |
261 |
if vertexes!=None: |
262 |
try:
|
263 |
for vertex in vertexes: |
264 |
v = createPoint(subtype, vertex) |
265 |
line.addVertex(v) |
266 |
except:
|
267 |
return None |
268 |
|
269 |
return line
|
270 |
|
271 |
def createMultiLine(subtype=D2, lines=None): |
272 |
"""
|
273 |
Create a new multiline with a subtype or None if can't create geometry
|
274 |
:return: multiline
|
275 |
:rtype: Geometry
|
276 |
"""
|
277 |
multiline = createGeometry(MULTILINE, subtype) |
278 |
if lines:
|
279 |
try:
|
280 |
for line in lines: |
281 |
if isinstance(line, Line): #,Line) |
282 |
pass
|
283 |
else:
|
284 |
line = createLine(subtype=subtype, vertexes=line) |
285 |
multiline.addPrimitive(line) |
286 |
except:
|
287 |
return None |
288 |
|
289 |
return multiline
|
290 |
|
291 |
def createEnvelope(pointMin=None, pointMax=None, dimension=2): |
292 |
"""
|
293 |
Returns envelope as a minimum bounding box or rectangle. This envelope is
|
294 |
equivalent to the GM_Envelope specified in ISO 19107.
|
295 |
:param pointMax:
|
296 |
:type pointMax: geometry POINT
|
297 |
:param pointMin:
|
298 |
:type pointMin: geometry POINT
|
299 |
:return: envelope
|
300 |
:rtype Geometry
|
301 |
"""
|
302 |
# Coord min point
|
303 |
get=getattr(pointMin,"__getitem__",None) |
304 |
if get != None: |
305 |
xmin = get(0)
|
306 |
ymin = get(1)
|
307 |
else:
|
308 |
xmin = pointMin.getX() |
309 |
ymin = pointMin.getY() |
310 |
|
311 |
# Coord max point
|
312 |
get=getattr(pointMax,"__getitem__",None) |
313 |
if get != None: |
314 |
xmax = get(0)
|
315 |
ymax = get(1)
|
316 |
else:
|
317 |
xmax = pointMax.getX() |
318 |
ymax = pointMax.getY() |
319 |
|
320 |
geometryManager = GeometryLocator.getGeometryManager() |
321 |
if pointMax!=None and pointMin!=None: |
322 |
envelope = geometryManager.createEnvelope(xmin, ymin, xmax, ymax, dimension) |
323 |
else:
|
324 |
envelope = geometryManager.createEnvelope() |
325 |
|
326 |
return envelope
|
327 |
|
328 |
|
329 |
def createGeometryFromWKT(WKT): |
330 |
"""
|
331 |
Create geometry from WKT string
|
332 |
:param WKT
|
333 |
:type WKT: string
|
334 |
:return: geometry
|
335 |
:rtype: Geometry
|
336 |
"""
|
337 |
geometryManager = GeometryLocator.getGeometryManager() |
338 |
geometry = geometryManager.createFrom(WKT) |
339 |
return geometry
|
340 |
|
341 |
def main(): |
342 |
pass
|