Revision 561 org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.app/org.gvsig.scripting.app.mainplugin/src/main/resources-plugin/scripting/lib/gvsig_2_3_0/utils.py
utils.py | ||
---|---|---|
6 | 6 |
from java.awt import Color |
7 | 7 |
|
8 | 8 |
def createFeatureType(schema = None): |
9 |
"""Returns attributes definition. If Schema is recived then makes a copy and
|
|
9 |
"""Returns attributes definition. If Schema is recived then makes a copy and |
|
10 | 10 |
returns editable instance. Otherwise returns empty Schema. |
11 | 11 |
:param schema: Schema to make a copy |
12 | 12 |
:type schema: Schema |
13 | 13 |
""" |
14 | 14 |
if isinstance(schema, FeatureType): |
15 |
try:
|
|
15 |
try: |
|
16 | 16 |
s = schema.getCopy().getEditable() |
17 | 17 |
return s |
18 | 18 |
except: |
19 | 19 |
pass |
20 |
|
|
20 |
|
|
21 | 21 |
application = ApplicationLocator.getManager() |
22 | 22 |
datamanager = application.getDataManager() |
23 | 23 |
return datamanager.createFeatureType() |
... | ... | |
29 | 29 |
Returns new layer |
30 | 30 |
:param schema: layer data definition |
31 | 31 |
:type schema: Schema |
32 |
:param servertype:
|
|
32 |
:param servertype: |
|
33 | 33 |
:type servertype: string |
34 | 34 |
:return: new layer |
35 | 35 |
:rtype: Layer |
... | ... | |
42 | 42 |
if layertype == "Shape" : |
43 | 43 |
if schema.get("GEOMETRY",None) == None: |
44 | 44 |
raise RuntimeException("Shape need a field named GEOMETRY in the schema") |
45 |
|
|
45 |
|
|
46 | 46 |
if parameters["geometryType"] == None: |
47 | 47 |
raise RuntimeException("Invalid geometry type for new layer") |
48 |
|
|
48 |
|
|
49 | 49 |
try: |
50 | 50 |
application = ApplicationLocator.getManager() |
51 | 51 |
datamanager = application.getDataManager() |
... | ... | |
68 | 68 |
layer = mapcontextmanager.createLayer(store.getName(), store) |
69 | 69 |
|
70 | 70 |
return layer |
71 |
|
|
71 |
|
|
72 | 72 |
except Throwable, ex: |
73 | 73 |
raise RuntimeException("Can't create layer, "+ str(ex)) |
74 | 74 |
|
75 |
def loadShapeFile(shpFile, CRS="CRS:84"):
|
|
75 |
def loadShapeFile(shpFile, **parameters):
|
|
76 | 76 |
""" |
77 | 77 |
Add existing shape file to the view. Returns Layer shape file |
78 | 78 |
:param shpFile: absolute file path |
... | ... | |
82 | 82 |
:return: the shape |
83 | 83 |
:type return: Layer |
84 | 84 |
""" |
85 |
layer = loadLayer('Shape', shpFile=shpFile, CRS=CRS) |
|
86 |
currentView().addLayer(layer) |
|
85 |
if parameters.get("CRS",None)==None: |
|
86 |
parameters["CRS"]="EPSG:4326" |
|
87 |
parameters["shpFile"]=shpFile |
|
88 |
layer = loadLayer('Shape', **parameters) |
|
89 |
viewName = parameters.get("gvViewName",None) |
|
90 |
if viewName == None: |
|
91 |
view = currentView() |
|
92 |
else: |
|
93 |
project = currentProject() |
|
94 |
view = project.getView(viewName) |
|
95 |
if view == None: |
|
96 |
raise RuntimeException("Can't get view with name '"+viewName+"', "+ str(ex)) |
|
97 |
view.addLayer(layer) |
|
87 | 98 |
layer.setActive(True) |
88 | 99 |
return layer |
89 |
|
|
100 |
|
|
101 |
def loadRasterFile(filename, **parameters): |
|
102 |
parameters["uri"]= File(filename).toURI() |
|
103 |
ext = os.path.splitext(filename) |
|
104 |
#if ext[1].lower() == ".jp2" or ext[1].lower()=="ecw": |
|
105 |
# layer = loadLayer("Ermapper Store", **parameters) |
|
106 |
#else: |
|
107 |
# layer = loadLayer('Gdal Store', **parameters) |
|
108 |
layer = loadLayer('Gdal Store', **parameters) |
|
109 |
viewName = parameters.get("gvViewName",None) |
|
110 |
if viewName == None: |
|
111 |
view = currentView() |
|
112 |
else: |
|
113 |
project = currentProject() |
|
114 |
view = project.getView(viewName) |
|
115 |
if view == None: |
|
116 |
raise RuntimeException("Can't get view with name '"+viewName+"', "+ str(ex)) |
|
117 |
view.addLayer(layer) |
|
118 |
layer.setActive(True) |
|
119 |
return layer |
|
120 |
|
|
90 | 121 |
def loadLayer(layerType, **parameters): |
91 | 122 |
try: |
92 | 123 |
application = ApplicationLocator.getManager() |
... | ... | |
94 | 125 |
mapcontextmanager = application.getMapContextManager() |
95 | 126 |
store_parameters = datamanager.createStoreParameters(layerType) |
96 | 127 |
copyToDynObject(parameters, store_parameters) |
97 |
store = datamanager.openStore(layerType, store_parameters) |
|
98 |
layer = mapcontextmanager.createLayer(store.getName(), store) |
|
128 |
store = datamanager.openStore(layerType, store_parameters) |
|
129 |
layerName = parameters.get("gvLayerName", store.getName()) |
|
130 |
layer = mapcontextmanager.createLayer(layerName, store) |
|
99 | 131 |
except Throwable, ex: |
100 |
raise RuntimeException("Can't load layer, "+ str(ex))
|
|
132 |
raise RuntimeException("Can't load layer, "+ str(ex)) |
|
101 | 133 |
|
102 | 134 |
return layer |
103 |
|
|
104 |
def createShape(definition, filename, geometryType, CRS="CRS:84"): |
|
135 |
|
|
136 |
def createShape(definition, filename, geometryType=None, CRS="CRS:84"):
|
|
105 | 137 |
""" |
106 |
Return new shape layer
|
|
138 |
Return new shape layer |
|
107 | 139 |
:param definition: layer data definition |
108 | 140 |
:type definition: Schema |
109 |
:param filename: absolute path for shape files.
|
|
141 |
:param filename: absolute path for shape files. |
|
110 | 142 |
:type filename: string |
111 | 143 |
:param geometryType: geometry type for shape |
112 | 144 |
:type geometryType: string |
113 | 145 |
:return: new shape layer |
114 | 146 |
:rtype: Layer |
115 | 147 |
""" |
148 |
geomattr = definition.getDefaultGeometryAttribute() |
|
149 |
if geomattr == None: |
|
150 |
raise RuntimeException("Can't create a shp without geometry attribute") |
|
151 |
if geometryType == None : |
|
152 |
geometryType = geomattr.getGeomType().getType() |
|
153 |
else: |
|
154 |
if geometryType != geomattr.getGeomType().getType(): |
|
155 |
raise RuntimeException("Can't create a shp, geoemtry mismatch.") |
|
156 |
|
|
116 | 157 |
return createLayer( |
117 |
definition,
|
|
118 |
"FilesystemExplorer",
|
|
119 |
"Shape",
|
|
120 |
shpFile=filename,
|
|
158 |
definition, |
|
159 |
"FilesystemExplorer", |
|
160 |
"Shape", |
|
161 |
shpFile=filename, |
|
121 | 162 |
CRS=CRS, |
122 | 163 |
geometryType = geometryType |
123 | 164 |
) |
... | ... | |
131 | 172 |
try: |
132 | 173 |
application = ApplicationLocator.getManager() |
133 | 174 |
datamanager = application.getDataManager() |
134 |
|
|
175 |
|
|
135 | 176 |
server_parameters = datamanager.createServerExplorerParameters(servertype) |
136 | 177 |
copyToDynObject(parameters, server_parameters) |
137 | 178 |
|
138 | 179 |
server = datamanager.openServerExplorer(servertype, server_parameters) |
139 |
|
|
180 |
|
|
140 | 181 |
store_parameters = server.getAddParameters(tableType) |
141 | 182 |
copyToDynObject(parameters, store_parameters) |
142 | 183 |
store_parameters.setDefaultFeatureType(schema()) |
143 |
|
|
184 |
|
|
144 | 185 |
server.add(tableType, store_parameters, True) |
145 | 186 |
|
146 | 187 |
store = datamanager.openStore(tableType, store_parameters) |
147 |
|
|
188 |
|
|
148 | 189 |
return store |
149 |
|
|
190 |
|
|
150 | 191 |
except Throwable, ex: |
151 | 192 |
raise RuntimeException("Can't create table, "+ str(ex)) |
152 |
|
|
193 |
|
|
153 | 194 |
def createDBF(definition, DbfFile, CRS="CRS:84"): |
154 | 195 |
""" |
155 |
Creates a new dbf document
|
|
196 |
Creates a new dbf document |
|
156 | 197 |
:param definition: layer data definition |
157 | 198 |
:type definition: Schema |
158 |
:param DbfFile: absolute path for shape files.
|
|
199 |
:param DbfFile: absolute path for shape files. |
|
159 | 200 |
:type DbfFile: string |
160 | 201 |
:return: new dbf |
161 |
:rtype: Table
|
|
202 |
:rtype: Table |
|
162 | 203 |
""" |
163 | 204 |
return createTable( |
164 |
definition,
|
|
165 |
"FilesystemExplorer",
|
|
166 |
"DBF",
|
|
167 |
DbfFile=DbfFile,
|
|
205 |
definition, |
|
206 |
"FilesystemExplorer", |
|
207 |
"DBF", |
|
208 |
DbfFile=DbfFile, |
|
168 | 209 |
CRS=CRS, |
169 | 210 |
) |
170 | 211 |
|
171 | 212 |
|
172 | 213 |
#=====================# |
173 | 214 |
# Simbology Functions # |
174 |
#=====================#
|
|
215 |
#=====================# |
|
175 | 216 |
COLORS = { |
176 | 217 |
'black': Color.black, |
177 | 218 |
'blue': Color.blue, |
... | ... | |
187 | 228 |
'white': Color.white, |
188 | 229 |
'yellow': Color.yellow, |
189 | 230 |
} |
190 |
|
|
231 |
|
|
191 | 232 |
def simplePointSymbol(color=None): |
192 | 233 |
""" |
193 |
Returns simple point symbol using parameter color. If no color
|
|
234 |
Returns simple point symbol using parameter color. If no color |
|
194 | 235 |
use a ramdom color |
195 | 236 |
:param color: String color name or Java awt Color |
196 | 237 |
:return: gvSIG point symbol |
197 | 238 |
""" |
198 | 239 |
if isinstance(color, str) and COLORS.has_key(color.lower()): |
199 | 240 |
color = COLORS.get(color) |
200 |
|
|
241 |
|
|
201 | 242 |
if not isinstance(color, Color): |
202 | 243 |
color = getDefaultColor() |
203 |
|
|
244 |
|
|
204 | 245 |
return MapContextLocator.getSymbolManager().createSymbol( |
205 | 246 |
Geometry.TYPES.POINT, color) |
206 | 247 |
|
207 | 248 |
def simpleLineSymbol(color=None): |
208 | 249 |
""" |
209 |
Returns simple line symbol using parameter color. If no color use a
|
|
210 |
ramdom color
|
|
250 |
Returns simple line symbol using parameter color. If no color use a |
|
251 |
ramdom color |
|
211 | 252 |
:param color: String color name or Java awt Color |
212 | 253 |
:return: gvSIG line symbol |
213 |
|
|
254 |
|
|
214 | 255 |
""" |
215 | 256 |
if isinstance(color, str) and COLORS.has_key(color.lower()): |
216 | 257 |
color = COLORS.get(color) |
217 | 258 |
|
218 | 259 |
if not isinstance(color, Color): |
219 | 260 |
color = getDefaultColor() |
220 |
|
|
261 |
|
|
221 | 262 |
return MapContextLocator.getSymbolManager().createSymbol( |
222 | 263 |
Geometry.TYPES.CURVE, color) |
223 | 264 |
|
224 | 265 |
def simplePolygonSymbol(color = None): |
225 | 266 |
""" |
226 |
Returns simple polygon symbol using parameter color. If no color
|
|
227 |
use a ramdom color.
|
|
267 |
Returns simple polygon symbol using parameter color. If no color |
|
268 |
use a ramdom color. |
|
228 | 269 |
:param color: String color name or Java awt Color |
229 | 270 |
:return: gvSIG polygon symbol |
230 | 271 |
""" |
... | ... | |
233 | 274 |
|
234 | 275 |
if not isinstance(color, Color): |
235 | 276 |
color = getDefaultColor() |
236 |
|
|
277 |
|
|
237 | 278 |
return MapContextLocator.getSymbolManager().createSymbol( |
238 | 279 |
Geometry.TYPES.SURFACE, color) |
239 |
|
|
240 | 280 |
|
281 |
|
|
241 | 282 |
#=========================================# |
242 | 283 |
# gvSIG Application Preferences Functions # |
243 |
#=========================================#
|
|
284 |
#=========================================# |
|
244 | 285 |
|
245 | 286 |
def getDataFolder(): |
246 | 287 |
""" |
247 |
Returns gvSIG data folder. This folder is defined in application
|
|
288 |
Returns gvSIG data folder. This folder is defined in application |
|
248 | 289 |
preferences. If is not defined returns None. |
249 | 290 |
""" |
250 | 291 |
return Preferences.userRoot().node("gvsig.foldering").get('DataFolder', None) |
251 |
|
|
292 |
|
|
252 | 293 |
def getProjectsFolder(): |
253 | 294 |
""" |
254 |
Returns gvSIG projects folder. This folder is defined in application
|
|
295 |
Returns gvSIG projects folder. This folder is defined in application |
|
255 | 296 |
preferences. If is not defined returns None. |
256 | 297 |
""" |
257 | 298 |
return Preferences.userRoot().node("gvsig.foldering").get( |
... | ... | |
259 | 300 |
|
260 | 301 |
def getColorFromRGB(r, g, b, a=None): |
261 | 302 |
""" |
262 |
Returns an sRGB color with the specified red, green, blue, and alpha
|
|
303 |
Returns an sRGB color with the specified red, green, blue, and alpha |
|
263 | 304 |
(optional) values in the range (0 - 255). |
264 | 305 |
""" |
265 | 306 |
if a: |
266 | 307 |
color = Color(r, g, b, a) |
267 | 308 |
else: |
268 | 309 |
color = Color(r, g, b) |
269 |
|
|
310 |
|
|
270 | 311 |
return color |
271 |
|
|
312 |
|
|
272 | 313 |
def getDefaultColor(c = None): |
273 | 314 |
"""Returns gvsig default symbol fill color or ramdom color""" |
274 | 315 |
if MapContextLocator.getSymbolManager().isDefaultSymbolFillColorAleatory(): |
275 |
color = Color(random.randint(0-255),
|
|
276 |
random.randint(0-255),
|
|
316 |
color = Color(random.randint(0-255), |
|
317 |
random.randint(0-255), |
|
277 | 318 |
random.randint(0-255) |
278 | 319 |
) |
279 | 320 |
else: |
280 | 321 |
sp = MapContextLocator.getSymbolManager().getSymbolPreferences() |
281 |
color = sp.getDefaultSymbolFillColor()
|
|
322 |
color = sp.getDefaultSymbolFillColor() |
|
282 | 323 |
|
283 |
return color
|
|
284 |
|
|
324 |
return color |
|
325 |
|
|
285 | 326 |
#================# |
286 | 327 |
# OTHER # |
287 | 328 |
#================# |
288 | 329 |
|
289 | 330 |
def getCRS(crs): |
290 |
"""Returns Projection from string code (i.e. CRS:84) if exist or None if
|
|
291 |
not.
|
|
331 |
"""Returns Projection from string code (i.e. CRS:84) if exist or None if |
|
332 |
not. |
|
292 | 333 |
""" |
293 | 334 |
try: |
294 | 335 |
return CRSFactory.getCRS(crs) |
... | ... | |
308 | 349 |
# ==================================== |
309 | 350 |
# |
310 | 351 |
|
352 |
""" |
|
353 |
def cloneShape(layer, target=None, expresion = None, sortby="", asc=True): |
|
354 |
if target==None: |
|
355 |
# una capa temporal en el temp de andami |
|
356 |
#... |
|
357 |
else: |
|
358 |
targetfile = File(target) |
|
359 |
if targetfile.isAbsolute(): |
|
360 |
#La guardamos ahi mismo |
|
361 |
else: |
|
362 |
# Al temp de andami con ese nombre. |
|
363 |
|
|
364 |
# Crear la nueva capa |
|
365 |
|
|
366 |
return newLayer |
|
367 |
""" |
Also available in: Unified diff