Revision 383 org.gvsig.scripting.app/trunk/org.gvsig.scripting.app/org.gvsig.scripting.app.extension/src/main/resources/scripting/lib/gvsig.py
gvsig.py | ||
---|---|---|
26 | 26 |
__author__ = """Antonio Carrasco Valero |
27 | 27 |
Model Driven Development sl and Antonio Carrasco Valero |
28 | 28 |
<carrasco@modeldd.org> |
29 |
Victor Acevedo Royer <vacevedor@gmail.com> |
|
29 |
Victor Acevedo Royer <vacevedor@gvsig.com>, <vacevedor@gmail.com>
|
|
30 | 30 |
""" |
31 | 31 |
|
32 | 32 |
__docformat__ = 'plaintext' |
... | ... | |
35 | 35 |
from java.lang import RuntimeException |
36 | 36 |
from java.lang import Throwable |
37 | 37 |
|
38 |
from java.util.prefs import Preferences |
|
39 |
|
|
38 | 40 |
from org.gvsig.app import ApplicationLocator |
39 | 41 |
from org.gvsig.app.project.documents.view import ViewDocument |
40 | 42 |
|
41 | 43 |
from org.gvsig.fmap.mapcontext import MapContextLocator |
42 | 44 |
from org.gvsig.fmap.mapcontext.layers import FLayers |
43 | 45 |
from org.gvsig.fmap.dal import DALLocator, DataTypes |
44 |
from org.gvsig.fmap.dal.feature import EditableFeature, EditableFeatureType
|
|
45 |
#from org.gvsig.fmap.geom import Geometry, GeometryLocator
|
|
46 |
from org.gvsig.fmap.dal.feature import EditableFeature, EditableFeatureType, FeatureQueryOrder
|
|
47 |
from org.gvsig.fmap.dal.feature.impl import DefaultEditableFeature
|
|
46 | 48 |
|
47 | 49 |
from org.gvsig.tools import ToolsLocator |
48 | 50 |
|
... | ... | |
108 | 110 |
if name == None: |
109 | 111 |
try: |
110 | 112 |
activeDocument = self.getActiveDocument() |
111 |
#if v.__class__ == "project.document.view2d": |
|
112 | 113 |
if activeDocument.getTypeName() == "project.document.view2d": |
113 | 114 |
v = View(activeDocument) |
114 | 115 |
except Exception, ex: |
... | ... | |
146 | 147 |
t = Table(table) |
147 | 148 |
|
148 | 149 |
return t |
150 |
|
|
151 |
def getProjectionCode(self): |
|
152 |
return self.getProjection().getFullCode() |
|
149 | 153 |
|
150 |
|
|
151 | 154 |
class View(WrapperToJava): |
152 | 155 |
""" |
153 | 156 |
Represents gvSIG view document. |
... | ... | |
209 | 212 |
""" |
210 | 213 |
return self.getMapContext().getGraphicsLayer() |
211 | 214 |
|
212 |
|
|
215 |
def getProjectionCode(self): |
|
216 |
return self.getProjection().getFullCode() |
|
217 |
|
|
213 | 218 |
class Layers(WrapperToJava): |
214 | 219 |
""" |
215 | 220 |
Iterable layers set |
... | ... | |
230 | 235 |
|
231 | 236 |
def __init__(self,layers): |
232 | 237 |
self.__layers = layers |
233 |
self.__index = 0
|
|
238 |
self.__index = -1
|
|
234 | 239 |
|
235 | 240 |
def next(self): |
236 |
if self.__index > self.__layers.getLayersCount() : |
|
241 |
self.__index+=1 |
|
242 |
if self.__index >= self.__layers.getLayersCount() : |
|
237 | 243 |
raise StopIteration() |
238 |
return Layer(self.__layers.get(self.__index)) |
|
244 |
return Layer(self.__layers.getLayer(self.__index))
|
|
239 | 245 |
|
240 | 246 |
class Store(WrapperToJava): |
241 | 247 |
""" |
... | ... | |
246 | 252 |
#self.data = None |
247 | 253 |
self.fset = None |
248 | 254 |
|
249 |
def features(self, expresion = None): |
|
255 |
def features(self, expresion = None, sortBy="", asc=True):
|
|
250 | 256 |
""" |
251 |
Return layer features set |
|
257 |
Return layer features set (FeatureSet)
|
|
252 | 258 |
|
253 | 259 |
:param expresion: filter to apply to the feature set to select |
254 | 260 |
determinates features that match with expression |
255 | 261 |
:type expresion: string |
262 |
:param sortBy: name of attribute to sort |
|
263 |
:type sortby: string |
|
264 |
:param: asc: order |
|
265 |
:type asc: boolean |
|
256 | 266 |
:return: FeatureSet |
257 | 267 |
""" |
258 | 268 |
|
259 |
if expresion == None: |
|
269 |
if expresion == None and sortBy =="":
|
|
260 | 270 |
self.fset = self.getFeatureSet() |
261 | 271 |
else: |
262 | 272 |
application = ApplicationLocator.getManager() |
263 | 273 |
datamanager = application.getDataManager() |
264 | 274 |
query = self.createFeatureQuery() |
265 |
query.setFilter( datamanager.createExpresion(expresion) ) |
|
275 |
if sortBy != "": |
|
276 |
order = FeatureQueryOrder() |
|
277 |
order.add(sortBy, asc) |
|
278 |
query.setOrder(order) |
|
279 |
if expresion != None: |
|
280 |
query.setFilter(datamanager.createExpresion(expresion)) |
|
266 | 281 |
#self.fset = data.getFeatureSet(query) |
267 | 282 |
self.fset = self.getFeatureSet(query) |
268 | 283 |
|
269 |
return FeatureSet(self.fset) |
|
284 |
return FeatureSet(self.fset)
|
|
270 | 285 |
|
271 | 286 |
def edit(self): |
272 | 287 |
""" |
... | ... | |
318 | 333 |
:param editableFeature: editableFeature |
319 | 334 |
:type editableFeature: Java editableFeature |
320 | 335 |
""" |
321 |
#fset = self.data.getFeatureSet() |
|
322 |
#fset.update(editableFeature) |
|
323 | 336 |
if not isinstance(feature, EditableFeature): |
324 | 337 |
feature = feature._javaobj |
325 | 338 |
self.fset.update(feature) |
... | ... | |
339 | 352 |
except Throwable, ex: |
340 | 353 |
self.abort() |
341 | 354 |
raise Throwable("Can't finish layer edition, cancelling changes. %s" % repr(ex)) |
342 |
|
|
343 | 355 |
|
344 | 356 |
def abort(self): |
345 | 357 |
""" |
346 | 358 |
Cancel layer edition |
347 | 359 |
""" |
348 |
#data = self.getDataStore() |
|
349 | 360 |
self.cancelEditing() |
350 | 361 |
self.dispose() |
351 | 362 |
|
... | ... | |
353 | 364 |
""" |
354 | 365 |
Return layer feature selected set |
355 | 366 |
""" |
356 |
return self().getSelection()
|
|
367 |
return FeatureSet(self().getSelection())
|
|
357 | 368 |
|
358 | 369 |
class __DefaultTable__(WrapperToJava): |
359 | 370 |
def __init__(self,document, data): |
360 | 371 |
self.data = Store(data) |
361 | 372 |
WrapperToJava.__init__(self,document) |
362 | 373 |
|
363 |
def features(self, expresion = None): |
|
374 |
def features(self, expresion = None, sortBy="", asc=True):
|
|
364 | 375 |
""" |
365 | 376 |
Return table features set |
366 | 377 |
|
... | ... | |
370 | 381 |
:return: FeatureSet |
371 | 382 |
""" |
372 | 383 |
|
373 |
return self.data.features(expresion = None)
|
|
384 |
return self.data.features(expresion, sortBy, asc)
|
|
374 | 385 |
|
375 | 386 |
def edit(self): |
376 | 387 |
""" |
... | ... | |
424 | 435 |
Return layer feature selected set |
425 | 436 |
""" |
426 | 437 |
return self.data.getSelection() |
438 |
|
|
439 |
def getProjectionCode(self): |
|
440 |
""" |
|
441 |
Return layer projection code |
|
442 |
""" |
|
443 |
return self.getProjection().getFullCode() |
|
427 | 444 |
|
428 | 445 |
class Table(__DefaultTable__): |
429 | 446 |
""" |
... | ... | |
439 | 456 |
def __init__(self,layer): |
440 | 457 |
__DefaultTable__.__init__(self, layer, layer.getFeatureStore()) |
441 | 458 |
#WrapperToJava.__init__(self,layer) |
459 |
|
|
460 |
def getTypeVectorLayer(self): |
|
461 |
return self().getTypeVectorLayer() |
|
442 | 462 |
|
443 | 463 |
class FeatureSet(WrapperToJava): |
444 | 464 |
def __init__(self,featureSet): |
... | ... | |
455 | 475 |
:type editableFeature: Java editableFeature |
456 | 476 |
""" |
457 | 477 |
#FIXME |
458 |
if not isinstance(feature, EditableFeature) and \ |
|
459 |
feature.__class__ != "org.gvsig.fmap.dal.feature.impl.DefaultEditableFeature": |
|
478 |
if not isinstance(feature, EditableFeature) and not isinstance(feature, DefaultEditableFeature): |
|
460 | 479 |
feature = feature._javaobj |
461 | 480 |
self().update(feature) |
462 | 481 |
|
... | ... | |
506 | 525 |
if not isinstance(self._javaobj, EditableFeature): |
507 | 526 |
self.featureNoEditable = self._javaobj |
508 | 527 |
self._javaobj = self._javaobj.getEditable() |
509 |
|
|
510 |
#return self.getEditable() |
|
511 | 528 |
|
512 | 529 |
def __getitem__(self,key): |
513 | 530 |
return self.get(key) |
... | ... | |
599 | 616 |
return self.getAttributeDescriptor(name) |
600 | 617 |
|
601 | 618 |
def get(self, name, default=None): |
619 |
""" |
|
620 |
Return a feature attribute descriptor that contains information about |
|
621 |
one of the attributes in a feature, such as its name, data type or |
|
622 |
precision. |
|
623 |
:param name: Attribute name |
|
624 |
:type name: string |
|
625 |
:param default: Value to return if no attribute name found. |
|
626 |
:return: AttributeDescriptor |
|
627 |
""" |
|
602 | 628 |
x = self.getAttributeDescriptor(name) |
603 | 629 |
if x == None: |
604 | 630 |
return default |
605 | 631 |
return x |
606 | 632 |
|
633 |
def getListAttrNames(self): |
|
634 |
""" |
|
635 |
Return dictionary with atribute names as dict keys and None as values |
|
636 |
""" |
|
637 |
l = list() |
|
638 |
for attr in self.getAttributeDescriptors(): |
|
639 |
l.append(attr.getName()) |
|
640 |
|
|
641 |
return l |
|
642 |
|
|
643 |
def getCopy(self): |
|
644 |
return Schema(self().getCopy()) |
|
645 |
|
|
607 | 646 |
def modify(self): |
608 |
#FIXME comprobar si es un DefaultFeatureType |
|
609 | 647 |
if not isinstance(self._javaobj, EditableFeatureType): |
610 | 648 |
self.featureTypeNoEditable = self._javaobj |
611 | 649 |
self._javaobj = self._javaobj.getEditable() |
612 |
|
|
650 |
|
|
651 |
|
|
652 |
#=====================# |
|
653 |
# Vectorial Functions # |
|
654 |
#=====================# |
|
655 |
|
|
613 | 656 |
def createSchema(schema = None): |
614 | 657 |
""" |
615 | 658 |
Return empty layer definition |
616 | 659 |
""" |
617 | 660 |
if schema != None: |
618 |
return Schema(schema.getCopy()) |
|
661 |
s = schema.getCopy() |
|
662 |
s.modify() |
|
663 |
return s |
|
619 | 664 |
|
620 | 665 |
application = ApplicationLocator.getManager() |
621 | 666 |
datamanager = application.getDataManager() |
... | ... | |
723 | 768 |
|
724 | 769 |
def createDBF(definition, DbfFile, CRS="wsg86"): |
725 | 770 |
""" |
726 |
Create a new shape layer
|
|
771 |
Create a new dbf document
|
|
727 | 772 |
|
728 | 773 |
:param definition: layer data definition |
729 | 774 |
:type definition: Schema |
730 | 775 |
:param DbfFile: absolute path for shape files. |
731 | 776 |
:type DbfFile: string |
732 |
:param geometryType: geometry type for shape |
|
733 |
:type geometryType: string |
|
734 |
:return: new shape layer |
|
735 |
:rtype: Layer |
|
777 |
:return: new dbf |
|
778 |
:rtype: Table |
|
736 | 779 |
""" |
737 | 780 |
return createTable( |
738 | 781 |
definition, |
... | ... | |
741 | 784 |
DbfFile=DbfFile, |
742 | 785 |
CRS=CRS, |
743 | 786 |
) |
744 |
|
|
787 |
|
|
788 |
#=====================# |
|
789 |
# Documents Functions # |
|
790 |
#=====================# |
|
791 |
|
|
745 | 792 |
def currentProject(): |
746 | 793 |
""" |
747 | 794 |
Return the current gvSIG proyect |
... | ... | |
770 | 817 |
|
771 | 818 |
return None |
772 | 819 |
|
820 |
def currentTable(): |
|
821 |
""" |
|
822 |
Return the current active table document or None |
|
823 |
|
|
824 |
:return: Table or None |
|
825 |
""" |
|
826 |
return Table(currentProject().getTable()) |
|
827 |
|
|
773 | 828 |
def currentView(): |
774 | 829 |
""" |
775 | 830 |
Return the current active view document or None |
... | ... | |
788 | 843 |
|
789 | 844 |
return currentView().getLayer() |
790 | 845 |
|
791 |
def SimplePointSymbol(color): |
|
846 |
#=====================# |
|
847 |
# Simbology Functions # |
|
848 |
#=====================# |
|
849 |
|
|
850 |
def simplePointSymbol(color): |
|
792 | 851 |
""" |
793 | 852 |
Return simple point symbol using parameter color |
794 | 853 |
|
... | ... | |
797 | 856 |
""" |
798 | 857 |
return MapContextLocator.getSymbolManager().createSymbol(Geometry.TYPES.POINT, color) |
799 | 858 |
|
800 |
def SimpleLineSymbol(color):
|
|
859 |
def simpleLineSymbol(color):
|
|
801 | 860 |
""" |
802 | 861 |
Return simple line symbol using parameter color |
803 | 862 |
|
... | ... | |
806 | 865 |
""" |
807 | 866 |
return MapContextLocator.getSymbolManager().createSymbol(Geometry.TYPES.CURVE, color) |
808 | 867 |
|
809 |
def SimplePoligonSymbol(color):
|
|
868 |
def simplePoligonSymbol(color):
|
|
810 | 869 |
""" |
811 | 870 |
Return simple poligon symbol using parameter color |
812 | 871 |
|
... | ... | |
816 | 875 |
return MapContextLocator.getSymbolManager().createSymbol(Geometry.TYPES.SURFACE, color) |
817 | 876 |
|
818 | 877 |
|
878 |
#=========================================# |
|
879 |
# gvSIG Application Preferences Functions # |
|
880 |
#=========================================# |
|
881 |
|
|
882 |
def getDataFolder(): |
|
883 |
""" |
|
884 |
Return gvSIG data folder. This folder is defined in application preferences |
|
885 |
If not defined return None |
|
886 |
""" |
|
887 |
|
|
888 |
return Preferences.userRoot().node("gvsig.foldering").get('DataFolder', None) |
|
889 |
|
|
890 |
def getProjectsFolder(): |
|
891 |
""" |
|
892 |
Return gvSIG projects folder. This folder is defined in application preferences. |
|
893 |
If not defined return None |
|
894 |
""" |
|
895 |
|
|
896 |
return Preferences.userRoot().node("gvsig.foldering").get('ProjectsFolder', None) |
|
897 |
|
|
819 | 898 |
def copyToDynObject(values, target): |
820 | 899 |
definition = target.getDynClass(); |
821 | 900 |
fields = definition.getDynFields(); |
Also available in: Unified diff