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
# -*- 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
"""
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.fmap.geom.primitive import Point
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
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
def createGeometry(type, subtype=D2):
76
    """
77
    Returns a new geometry with a concrete type and subtype or None if can't 
78
    create geometry.
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
    try:
87
        geometryManager = GeometryLocator.getGeometryManager()
88
        geometry = geometryManager.create(type, subtype)
89
    except:
90
        return None
91
    return geometry
92
  
93
def createPoint(x=0, y=0, subtype=D2):
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
97
    :param x: X coordinate value 
98
    :param y: Y coordinate value
99
    :type x: double
100
    :type y: double
101
    :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
111

    
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.
118
    :param points: list of points
119
    :type points: list
120
    :return: multipoint
121
    :rtype: multipoint
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
130
    
131
    return multipoint 
132

    
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
    :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
147

    
148
def createMultiPolygon(subtype=D2, polygons=None):  
149
    """
150
    Returns a new multipolygon with a subtype. or None if can't create geometry
151
    :return: multipolygon
152
    :rtype: Geometry
153
    """  
154
    multipolygon = createGeometry(MULTISURFACE, subtype)
155
    return multipolygon  
156
  
157
def createLine(subtype=D2, vertexes=None):  
158
    """
159
    Returns a new line with a subtype or None if can't create geometry
160
    :return: polygon
161
    :rtype: Geometry
162
    """  
163
    line = createGeometry(CURVE, subtype)
164
    return line
165

    
166
def createMultiLine(subtype=D2, lines = None):  
167
    """
168
    Create a new multiline with a subtype or None if can't create geometry
169
    :return: multiline
170
    :rtype: Geometry
171
    """  
172
    multiline = createGeometry(MULTILINE, subtype)
173
    return multiline
174
  
175
def createEnvelope(pointMax=None, pointMin=None, dimension=2):
176
    """
177
    Returns envelope as a minimum bounding box or rectangle. This envelope is 
178
    equivalent to the GM_Envelope specified in ISO 19107. 
179
    :param pointMax: 
180
    :type pointMax: geometry POINT
181
    :param pointMin: 
182
    :type pointMin: geometry POINT
183
    """
184
    geometryManager = GeometryLocator.getGeometryManager()
185
  
186
    if all((pointMax, pointMin)):
187
        envelope = geometryManager.createEnvelope(pointMax, pointMin)
188
    else:
189
        envelope = geometryManager.createEnvelope()    
190
  
191
    return envelope
192
    
193

    
194
def main():
195
  point = createPoint(1,1)