Revision 423
org.gvsig.scripting.app/trunk/org.gvsig.scripting.app/org.gvsig.scripting.app.extension/src/main/resources/scripting/lib/geom.py | ||
---|---|---|
23 | 23 |
# |
24 | 24 |
# |
25 | 25 |
|
26 |
""" |
|
27 |
Utility functions to manage gvSIG geometries |
|
28 |
""" |
|
29 |
|
|
26 | 30 |
__author__ = """Antonio Carrasco Valero |
27 | 31 |
Model Driven Development sl and Antonio Carrasco Valero |
28 | 32 |
<carrasco@modeldd.org> |
... | ... | |
33 | 37 |
|
34 | 38 |
|
35 | 39 |
from org.gvsig.fmap.geom import Geometry, GeometryLocator |
36 |
|
|
37 | 40 |
from org.gvsig.fmap.geom.primitive import Point |
38 |
|
|
39 | 41 |
from org.gvsig.tools import ToolsLocator |
40 | 42 |
|
41 | 43 |
|
... | ... | |
64 | 66 |
MULTIPOLYGON = Geometry.TYPES.MULTISURFACE |
65 | 67 |
|
66 | 68 |
# geometrySubTypes |
67 |
D2= Geometry.SUBTYPES.GEOM2D |
|
68 |
DM2= Geometry.SUBTYPES.GEOM2DM
|
|
69 |
D3= Geometry.SUBTYPES.GEOM3D |
|
70 |
DM3= Geometry.SUBTYPES.GEOM3DM
|
|
71 |
UNKNOWN= Geometry.SUBTYPES.UNKNOWN |
|
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
|
|
72 | 74 |
|
73 | 75 |
def createGeometry(type, subtype=D2): |
74 |
""" |
|
75 |
Create a new geometry with a concrete type and subtype
|
|
76 |
|
|
76 |
"""
|
|
77 |
Returns a new geometry with a concrete type and subtype or None if can't
|
|
78 |
create geometry. |
|
77 | 79 |
:param type: geometry type |
78 | 80 |
:type type: string |
79 | 81 |
:param subtype: geometry subtype |
80 | 82 |
:type type: string |
81 | 83 |
:return: geometry |
82 | 84 |
:rtype: geometry |
83 |
""" |
|
84 |
|
|
85 |
#type = getGeometryType(type) |
|
86 |
#subtype = getGeometrySubType(subtype) |
|
85 |
""" |
|
86 |
try: |
|
87 |
geometryManager = GeometryLocator.getGeometryManager() |
|
88 |
geometry = geometryManager.create(type, subtype) |
|
89 |
except: |
|
90 |
return None |
|
91 |
return geometry |
|
87 | 92 |
|
88 |
geometryManager = GeometryLocator.getGeometryManager() |
|
89 |
geometry = geometryManager.create(type, subtype) |
|
90 |
|
|
91 |
return geometry |
|
92 |
|
|
93 | 93 |
def createPoint(x=0, y=0, subtype=D2): |
94 |
|
|
95 |
""" |
|
96 |
Create a new point with a subtype and sets the value for the X and the Y |
|
97 |
|
|
94 |
""" |
|
95 |
Returns a new point with a subtype and sets the value for the X and the Y |
|
96 |
coordinates (default 0,0) or None if can't create point |
|
98 | 97 |
:param x: X coordinate value |
99 | 98 |
:param y: Y coordinate value |
100 | 99 |
:type x: double |
101 | 100 |
:type y: double |
102 | 101 |
:return: Point |
103 |
:rtype: Point |
|
104 |
""" |
|
105 |
|
|
106 |
geometryManager = GeometryLocator.getGeometryManager() |
|
107 |
point = geometryManager.createPoint(x, y, subtype) |
|
108 |
return point |
|
102 |
:rtype: Point |
|
103 |
""" |
|
104 |
try: |
|
105 |
geometryManager = GeometryLocator.getGeometryManager() |
|
106 |
point = geometryManager.createPoint(x, y, subtype) |
|
107 |
except: |
|
108 |
return None |
|
109 |
|
|
110 |
return point |
|
109 | 111 |
|
110 |
def createMultiPoint(points=None, subtype=D2):
|
|
111 |
""" |
|
112 |
Create a new multipoint with a subtype from a list of Points instances. Also
|
|
113 |
values of X and Y tuples like ([x1, y1], [x2, y2], ...., [xn, yn]) are |
|
114 |
allowed. If not points returns empty multipoint geometry.
|
|
115 |
|
|
112 |
def createMultiPoint(subtype=D2, points=None):
|
|
113 |
"""
|
|
114 |
Returns a new multipoint with a subtype from a list of Points instances.
|
|
115 |
Also values of X and Y tuples like ([x1, y1], [x2, y2], ...., [xn, yn]) are
|
|
116 |
allowed. If not points returns empty multipoint geometry. If can't create
|
|
117 |
geometry return None. |
|
116 | 118 |
:param points: list of points |
117 | 119 |
:type points: list |
118 | 120 |
:return: multipoint |
119 | 121 |
:rtype: multipoint |
120 |
""" |
|
121 |
multipoint = createGeometry(MULTIPOINT, subtype) |
|
122 |
if points == None:
|
|
123 |
return multipoint
|
|
124 |
|
|
125 |
for point in points:
|
|
126 |
if not isinstance(point, Point):
|
|
127 |
point = createPoint(point[0], point[1], subtype)
|
|
122 |
"""
|
|
123 |
multipoint = createGeometry(MULTIPOINT, subtype)
|
|
124 |
if all(points, multipoint):
|
|
125 |
try:
|
|
126 |
for point in points: |
|
127 |
multipoint.addPrimitive(point)
|
|
128 |
except:
|
|
129 |
return None
|
|
128 | 130 |
|
129 |
multipoint.addPoint(point) |
|
130 |
|
|
131 |
return multipoint |
|
131 |
return multipoint |
|
132 | 132 |
|
133 |
def createPolygon(vertex=None, subtype=D2):
|
|
134 |
""" |
|
135 |
Create a new polygon with a subtype.
|
|
133 |
def createPolygon(subtype=D2, vertexes=None):
|
|
134 |
"""
|
|
135 |
Returns a new polygon with a subtype. Or None if can't create the geometry
|
|
136 | 136 |
:return: polygon |
137 |
:rtype: Geometry |
|
138 |
""" |
|
139 |
polygon = createGeometry(SURFACE, subtype) |
|
140 |
return polygon |
|
137 |
:rtype: Geometry |
|
138 |
""" |
|
139 |
polygon = createGeometry(SURFACE, subtype) |
|
140 |
if all(vertexes, polygon): |
|
141 |
for vertex in vertexes: |
|
142 |
polygon.addPoint(vertex) |
|
143 |
polygon.closePrimitive() |
|
144 |
except: |
|
145 |
return None |
|
146 |
return polygon |
|
141 | 147 |
|
142 |
def createMultiPolygon(vertex=None, subtype=D2):
|
|
143 |
""" |
|
144 |
Create a new multipolygon with a subtype.
|
|
148 |
def createMultiPolygon(subtype=D2, polygons=None):
|
|
149 |
"""
|
|
150 |
Returns a new multipolygon with a subtype. or None if can't create geometry
|
|
145 | 151 |
:return: multipolygon |
146 | 152 |
:rtype: Geometry |
147 |
""" |
|
148 |
multipolygon = createGeometry(MULTISURFACE, subtype) |
|
149 |
return multipolygon |
|
153 |
"""
|
|
154 |
multipolygon = createGeometry(MULTISURFACE, subtype)
|
|
155 |
return multipolygon
|
|
150 | 156 |
|
151 |
def createLine(subtype=D2): |
|
152 |
""" |
|
153 |
Create a new line with a subtype.
|
|
157 |
def createLine(subtype=D2, vertexes=None):
|
|
158 |
"""
|
|
159 |
Returns a new line with a subtype or None if can't create geometry
|
|
154 | 160 |
:return: polygon |
155 | 161 |
:rtype: Geometry |
156 |
""" |
|
157 |
line = createGeometry(CURVE, subtype) |
|
158 |
return line |
|
162 |
"""
|
|
163 |
line = createGeometry(CURVE, subtype)
|
|
164 |
return line
|
|
159 | 165 |
|
160 |
def createMultiLine(subtype=D2): |
|
161 |
""" |
|
162 |
Create a new multiline with a subtype.
|
|
166 |
def createMultiLine(subtype=D2, lines = None):
|
|
167 |
"""
|
|
168 |
Create a new multiline with a subtype or None if can't create geometry
|
|
163 | 169 |
:return: multiline |
164 | 170 |
:rtype: Geometry |
165 |
""" |
|
166 |
multiline = createGeometry(MULTICURVECURVE, subtype)
|
|
167 |
return multiline |
|
171 |
"""
|
|
172 |
multiline = createGeometry(MULTILINE, subtype)
|
|
173 |
return multiline
|
|
168 | 174 |
|
169 | 175 |
def createEnvelope(pointMax=None, pointMin=None, dimension=2): |
170 |
""" |
|
171 |
Create envelope as a minimum bounding box or rectangle. This envelope is |
|
172 |
equivalent to the GM_Envelope specified in ISO 19107. |
|
173 |
|
|
176 |
""" |
|
177 |
Returns envelope as a minimum bounding box or rectangle. This envelope is |
|
178 |
equivalent to the GM_Envelope specified in ISO 19107. |
|
174 | 179 |
:param pointMax: |
175 | 180 |
:type pointMax: geometry POINT |
176 | 181 |
:param pointMin: |
177 | 182 |
:type pointMin: geometry POINT |
178 |
""" |
|
179 |
geometryManager = GeometryLocator.getGeometryManager() |
|
183 |
"""
|
|
184 |
geometryManager = GeometryLocator.getGeometryManager()
|
|
180 | 185 |
|
181 |
if None in (pointMax, pointMin):
|
|
182 |
envelope = geometryManager.createEnvelope()
|
|
183 |
else: |
|
184 |
envelope = geometryManager.createEnvelope(pointMax, pointMin)
|
|
186 |
if all((pointMax, pointMin)):
|
|
187 |
envelope = geometryManager.createEnvelope(pointMax, pointMin)
|
|
188 |
else:
|
|
189 |
envelope = geometryManager.createEnvelope()
|
|
185 | 190 |
|
186 |
return envelope |
|
191 |
return envelope
|
|
187 | 192 |
|
188 | 193 |
|
189 | 194 |
def main(): |
Also available in: Unified diff