gvsig-scripting / org.gvsig.scripting.app / trunk / org.gvsig.scripting.app / org.gvsig.scripting.app.extension / src / main / resources / scripting / lib / geom.py @ 418
History | View | Annotate | Download (5.01 KB)
1 |
# -*- coding: utf-8 -*-
|
---|---|
2 |
#
|
3 |
# File: gvsig.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 |
__author__ = """Antonio Carrasco Valero
|
27 |
Model Driven Development sl and Antonio Carrasco Valero
|
28 |
<carrasco@modeldd.org>
|
29 |
Victor Acevedo Royer <vacevedo@gvsig.com> <vacevedor@gmail.com>
|
30 |
"""
|
31 |
|
32 |
__docformat__ = 'plaintext'
|
33 |
|
34 |
|
35 |
from org.gvsig.fmap.geom import Geometry, GeometryLocator |
36 |
|
37 |
from org.gvsig.fmap.geom.primitive import Point |
38 |
|
39 |
from org.gvsig.tools import ToolsLocator |
40 |
|
41 |
|
42 |
#GeometryTypes
|
43 |
AGGREGATE = Geometry.TYPES.AGGREGATE |
44 |
ARC = Geometry.TYPES.ARC |
45 |
CIRCLE = Geometry.TYPES.CIRCLE |
46 |
CURVE = Geometry.TYPES.CURVE |
47 |
ELLIPSE = Geometry.TYPES.ELLIPSE |
48 |
ELLIPTICARC = Geometry.TYPES.ELLIPTICARC |
49 |
GEOMETRY = Geometry.TYPES.GEOMETRY |
50 |
MULTICURVE = Geometry.TYPES.MULTICURVE |
51 |
MULTIPOINT = Geometry.TYPES.MULTIPOINT |
52 |
MULTISOLID = Geometry.TYPES.MULTISOLID |
53 |
MULTISURFACE = Geometry.TYPES.MULTISURFACE |
54 |
NULL = Geometry.TYPES.NULL |
55 |
POINT = Geometry.TYPES.POINT |
56 |
SOLID = Geometry.TYPES.SOLID |
57 |
SPLINE = Geometry.TYPES.SPLINE |
58 |
SURFACE = Geometry.TYPES.SURFACE |
59 |
|
60 |
# Common named geometry types
|
61 |
POLYGON = Geometry.TYPES.SURFACE |
62 |
LINE = Geometry.TYPES.CURVE |
63 |
MULTILINE = Geometry.TYPES.MULTICURVE |
64 |
MULTIPOLYGON = Geometry.TYPES.MULTISURFACE |
65 |
|
66 |
# 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 |
72 |
|
73 |
def createGeometry(type, subtype=D2): |
74 |
"""
|
75 |
Create a new geometry with a concrete type and subtype
|
76 |
|
77 |
:param type: geometry type
|
78 |
:type type: string
|
79 |
:param subtype: geometry subtype
|
80 |
:type type: string
|
81 |
:return: geometry
|
82 |
:rtype: geometry
|
83 |
"""
|
84 |
|
85 |
#type = getGeometryType(type)
|
86 |
#subtype = getGeometrySubType(subtype)
|
87 |
|
88 |
geometryManager = GeometryLocator.getGeometryManager() |
89 |
geometry = geometryManager.create(type, subtype)
|
90 |
|
91 |
return geometry
|
92 |
|
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 |
|
98 |
:param x: X coordinate value
|
99 |
:param y: Y coordinate value
|
100 |
:type x: double
|
101 |
:type y: double
|
102 |
:return: Point
|
103 |
:rtype: Point
|
104 |
"""
|
105 |
|
106 |
geometryManager = GeometryLocator.getGeometryManager() |
107 |
point = geometryManager.createPoint(x, y, subtype) |
108 |
return point
|
109 |
|
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 |
|
116 |
:param points: list of points
|
117 |
:type points: list
|
118 |
:return: multipoint
|
119 |
: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) |
128 |
|
129 |
multipoint.addPoint(point) |
130 |
|
131 |
return multipoint
|
132 |
|
133 |
def createPolygon(vertex=None, subtype=D2): |
134 |
"""
|
135 |
Create a new polygon with a subtype.
|
136 |
:return: polygon
|
137 |
:rtype: Geometry
|
138 |
"""
|
139 |
polygon = createGeometry(SURFACE, subtype) |
140 |
return polygon
|
141 |
|
142 |
def createMultiPolygon(vertex=None, subtype=D2): |
143 |
"""
|
144 |
Create a new multipolygon with a subtype.
|
145 |
:return: multipolygon
|
146 |
:rtype: Geometry
|
147 |
"""
|
148 |
multipolygon = createGeometry(MULTISURFACE, subtype) |
149 |
return multipolygon
|
150 |
|
151 |
def createLine(subtype=D2): |
152 |
"""
|
153 |
Create a new line with a subtype.
|
154 |
:return: polygon
|
155 |
:rtype: Geometry
|
156 |
"""
|
157 |
line = createGeometry(CURVE, subtype) |
158 |
return line
|
159 |
|
160 |
def createMultiLine(subtype=D2): |
161 |
"""
|
162 |
Create a new multiline with a subtype.
|
163 |
:return: multiline
|
164 |
:rtype: Geometry
|
165 |
"""
|
166 |
multiline = createGeometry(MULTICURVECURVE, subtype) |
167 |
return multiline
|
168 |
|
169 |
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 |
|
174 |
:param pointMax:
|
175 |
:type pointMax: geometry POINT
|
176 |
:param pointMin:
|
177 |
:type pointMin: geometry POINT
|
178 |
"""
|
179 |
geometryManager = GeometryLocator.getGeometryManager() |
180 |
|
181 |
if None in (pointMax, pointMin): |
182 |
envelope = geometryManager.createEnvelope() |
183 |
else:
|
184 |
envelope = geometryManager.createEnvelope(pointMax, pointMin) |
185 |
|
186 |
return envelope
|
187 |
|
188 |
|
189 |
def main(): |
190 |
point = createPoint(1,1) |