Statistics
| Revision:

gvsig-scripting / org.gvsig.scripting.app / trunk / org.gvsig.scripting.app / org.gvsig.scripting.app.extension / src / main / resources / scripting / lib / geom.py @ 423

History | View | Annotate | Download (5.63 KB)

1 365 vacevedo
# -*- 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 423 vacevedo
"""
27
Utility functions to manage gvSIG geometries
28
"""
29
30 365 vacevedo
__author__ = """Antonio Carrasco Valero
31
Model Driven Development sl and Antonio Carrasco Valero
32
<carrasco@modeldd.org>
33 400 vacevedo
Victor Acevedo Royer <vacevedo@gvsig.com> <vacevedor@gmail.com>
34 365 vacevedo
"""
35
36
__docformat__ = 'plaintext'
37
38
39 382 vacevedo
from org.gvsig.fmap.geom import Geometry, GeometryLocator
40
from org.gvsig.fmap.geom.primitive import Point
41 365 vacevedo
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 382 vacevedo
# Common named geometry types
63 400 vacevedo
POLYGON = Geometry.TYPES.SURFACE
64 382 vacevedo
LINE = Geometry.TYPES.CURVE
65
MULTILINE = Geometry.TYPES.MULTICURVE
66 400 vacevedo
MULTIPOLYGON = Geometry.TYPES.MULTISURFACE
67 382 vacevedo
68 365 vacevedo
# geometrySubTypes
69 423 vacevedo
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 365 vacevedo
75
def createGeometry(type, subtype=D2):
76 423 vacevedo
    """
77
    Returns a new geometry with a concrete type and subtype or None if can't
78
    create geometry.
79 365 vacevedo
    :param type: geometry type
80
    :type type: string
81
    :param subtype: geometry subtype
82
    :type type: string
83
    :return: geometry
84
    :rtype: geometry
85 423 vacevedo
    """
86
    try:
87
        geometryManager = GeometryLocator.getGeometryManager()
88
        geometry = geometryManager.create(type, subtype)
89
    except:
90
        return None
91
    return geometry
92 365 vacevedo
93 382 vacevedo
def createPoint(x=0, y=0, subtype=D2):
94 423 vacevedo
    """
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
97 365 vacevedo
    :param x: X coordinate value
98
    :param y: Y coordinate value
99
    :type x: double
100
    :type y: double
101
    :return: Point
102 423 vacevedo
    :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
111 365 vacevedo
112 423 vacevedo
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.
118 382 vacevedo
    :param points: list of points
119 365 vacevedo
    :type points: list
120
    :return: multipoint
121
    :rtype: multipoint
122 423 vacevedo
    """
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
130 382 vacevedo
131 423 vacevedo
    return multipoint
132 365 vacevedo
133 423 vacevedo
def createPolygon(subtype=D2, vertexes=None):
134
    """
135
    Returns a new polygon with a subtype. Or None if can't create the geometry
136 400 vacevedo
    :return: polygon
137 423 vacevedo
    :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
147 400 vacevedo
148 423 vacevedo
def createMultiPolygon(subtype=D2, polygons=None):
149
    """
150
    Returns a new multipolygon with a subtype. or None if can't create geometry
151 400 vacevedo
    :return: multipolygon
152
    :rtype: Geometry
153 423 vacevedo
    """
154
    multipolygon = createGeometry(MULTISURFACE, subtype)
155
    return multipolygon
156 400 vacevedo
157 423 vacevedo
def createLine(subtype=D2, vertexes=None):
158
    """
159
    Returns a new line with a subtype or None if can't create geometry
160 400 vacevedo
    :return: polygon
161
    :rtype: Geometry
162 423 vacevedo
    """
163
    line = createGeometry(CURVE, subtype)
164
    return line
165 400 vacevedo
166 423 vacevedo
def createMultiLine(subtype=D2, lines = None):
167
    """
168
    Create a new multiline with a subtype or None if can't create geometry
169 400 vacevedo
    :return: multiline
170
    :rtype: Geometry
171 423 vacevedo
    """
172
    multiline = createGeometry(MULTILINE, subtype)
173
    return multiline
174 400 vacevedo
175 382 vacevedo
def createEnvelope(pointMax=None, pointMin=None, dimension=2):
176 423 vacevedo
    """
177
    Returns envelope as a minimum bounding box or rectangle. This envelope is
178
    equivalent to the GM_Envelope specified in ISO 19107.
179 382 vacevedo
    :param pointMax:
180
    :type pointMax: geometry POINT
181
    :param pointMin:
182
    :type pointMin: geometry POINT
183 423 vacevedo
    """
184
    geometryManager = GeometryLocator.getGeometryManager()
185 382 vacevedo
186 423 vacevedo
    if all((pointMax, pointMin)):
187
        envelope = geometryManager.createEnvelope(pointMax, pointMin)
188
    else:
189
        envelope = geometryManager.createEnvelope()
190 382 vacevedo
191 423 vacevedo
    return envelope
192 400 vacevedo
193 382 vacevedo
194 365 vacevedo
def main():
195
  point = createPoint(1,1)