gvsig-scripting / org.gvsig.scripting.app / trunk / org.gvsig.scripting.app / org.gvsig.scripting.app.extension / src / main / resources / scripting / lib / geom.py @ 387
History | View | Annotate | Download (4.2 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 <vacevedor@gmail.com>
|
30 |
"""
|
31 |
|
32 |
__docformat__ = 'plaintext'
|
33 |
|
34 |
|
35 |
from org.gvsig.fmap.crs import CRSFactory |
36 |
|
37 |
from org.gvsig.fmap.geom import Geometry, GeometryLocator |
38 |
|
39 |
from org.gvsig.fmap.geom.primitive import Point |
40 |
|
41 |
from org.gvsig.tools import ToolsLocator |
42 |
|
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 |
POLIGON = Geometry.TYPES.SURFACE |
64 |
LINE = Geometry.TYPES.CURVE |
65 |
MULTILINE = Geometry.TYPES.MULTICURVE |
66 |
MULTIPOLIGON = Geometry.TYPES.MULTISURFACE |
67 |
|
68 |
# geometrySubTypes
|
69 |
D2= Geometry.SUBTYPES.GEOM2D |
70 |
DM2= Geometry.SUBTYPES.GEOM2DM |
71 |
D3= Geometry.SUBTYPES.GEOM3D |
72 |
DM3= Geometry.SUBTYPES.GEOM3DM |
73 |
UNKNOWN= Geometry.SUBTYPES.UNKNOWN |
74 |
|
75 |
def createGeometry(type, subtype=D2): |
76 |
"""
|
77 |
Create a new geometry with a concrete type and subtype
|
78 |
|
79 |
:param type: geometry type
|
80 |
:type type: string
|
81 |
:param subtype: geometry subtype
|
82 |
:type type: string
|
83 |
:return: geometry
|
84 |
:rtype: geometry
|
85 |
"""
|
86 |
|
87 |
#type = getGeometryType(type)
|
88 |
#subtype = getGeometrySubType(subtype)
|
89 |
|
90 |
geometryManager = GeometryLocator.getGeometryManager() |
91 |
geometry = geometryManager.create(type, subtype)
|
92 |
|
93 |
return geometry
|
94 |
|
95 |
def createPoint(x=0, y=0, subtype=D2): |
96 |
|
97 |
"""
|
98 |
Create a new point with a subtype and sets the value for the X and the Y
|
99 |
|
100 |
:param x: X coordinate value
|
101 |
:param y: Y coordinate value
|
102 |
:type x: double
|
103 |
:type y: double
|
104 |
:return: Point
|
105 |
:rtype: Point
|
106 |
"""
|
107 |
|
108 |
geometryManager = GeometryLocator.getGeometryManager() |
109 |
point = geometryManager.createPoint(x, y, subtype) |
110 |
return point
|
111 |
|
112 |
def createMultiPoint(points=None, subtype=D2): |
113 |
"""
|
114 |
Create a new multipoint with a subtype from a list of Points. Also values of
|
115 |
X and Y tuples like ([x1, y1], [x2, y2], ...., [xn, yn]) are allowed
|
116 |
|
117 |
:param points: list of points
|
118 |
:type points: list
|
119 |
:return: multipoint
|
120 |
:rtype: multipoint
|
121 |
"""
|
122 |
multipoint = createGeometry(MULTIPOINT, subtype) |
123 |
if points == None: |
124 |
return multipoint
|
125 |
|
126 |
for point in points: |
127 |
if not isinstance(point, Point): |
128 |
point = createPoint(point[0], point[1], subtype) |
129 |
|
130 |
multipoint.add(point) |
131 |
|
132 |
return multipoint
|
133 |
|
134 |
def createEnvelope(pointMax=None, pointMin=None, dimension=2): |
135 |
"""
|
136 |
Create envelope as a minimum bounding box or rectangle. This envelope is
|
137 |
equivalent to the GM_Envelope specified in ISO 19107.
|
138 |
|
139 |
:param pointMax:
|
140 |
:type pointMax: geometry POINT
|
141 |
:param pointMin:
|
142 |
:type pointMin: geometry POINT
|
143 |
"""
|
144 |
geometryManager = GeometryLocator.getGeometryManager() |
145 |
|
146 |
if None in (pointMax, pointMin): |
147 |
envelope = geometryManager.createEnvelope() |
148 |
else:
|
149 |
envelope = geometryManager.createEnvelope(pointMax, pointMin) |
150 |
|
151 |
return envelope
|
152 |
|
153 |
def getCRS(crs): |
154 |
return CRSFactory.getCRS(crs)
|
155 |
|
156 |
|
157 |
def main(): |
158 |
point = createPoint(1,1) |