def getProjectLayer(view,layer): """Get vector layer or raster""" try: return gvsig.currentProject().getView(view).getLayer(layer) except: for i in gvsig.currentProject().getView(view).getLayers(): if i.name == layer: return i
def loadShapeFileNew(shpFile, CRS='CRS:84', active= False, view=gvsig.currentView()): try: CRS = gvsig.currentProject().getProjectionCode() except: pass layer = gvsig.loadLayer('Shape', shpFile=shpFile, CRS=CRS) if isinstance(view,str): view = gvsig.currentProject().getView(view) view.addLayer(layer) layer.setActive(active) return gvsig.Layer(layer)
def testEnabledDocuments(*args): if gvsig.currentProject().getDocuments() > 1: for doc in gvsig.currentProject().getDocuments(): if isinstance(doc, FeatureTableDocumentPanel): print True elif isinstance(doc, TableDocument): print True elif isinstance(doc, DefaultViewDocument): layers = doc.getMapContext().hasLayers() return True print doc, type(doc) print False
def getProjectLayer(view, layer): """Get vector layer or raster""" try: if isinstance(view, str): view = gvsig.currentProject().getView(view) if isinstance(layer, str): return view.getLayer(layer) else: return layer except: #Bug: Raster problem with getLayer for i in gvsig.currentProject().getView(view).getLayers(): if i.name == layer: return i
def getProjectLayer(view,layer): """Get vector layer or raster""" try: if isinstance(view, str): view = gvsig.currentProject().getView(view) if isinstance(layer, str): return view.getLayer(layer) else: return layer except: #Bug: Raster problem with getLayer for i in gvsig.currentProject().getView(view).getLayers(): if i.name == layer: return i
def __defineParameters(self, algorithm, kwparams): """ Define input parameters """ params = algorithm.getParameters() for i in range(0,params.getNumberOfParameters()): param = params.getParameter(i) if param.getParameterName() in kwparams: paramValue = kwparams[param.getParameterName()] else: paramValue = kwparams[i] #Vector to SEXTANTE if param.getParameterTypeName() == "Vector Layer": if isinstance(paramValue, str): layer = gvsig.currentView().getLayer(paramValue) paramValue = self.__createSextanteLayer(layer()) else: paramValue = self.__createSextanteLayer(paramValue()) #Raster to SEXTANTE elif param.getParameterTypeName() == "Raster Layer": if isinstance(paramValue, str): layer = gvsig.currentView().getLayer(paramValue) paramValue = self.__createSextanteRaster(layer) else: paramValue = self.__createSextanteRaster(paramValue) #Table to SEXTANTE elif param.getParameterTypeName() == "Table": if isinstance(paramValue, str): layer = gvsig.currentProject().getTable(paramValue) paramValue = self.__createSextanteTable(layer()) else: paramValue = self.__createSextanteTable(paramValue()) #Set parameter value param.setParameterValue(paramValue)
def __defineParameters_table2sextante(self, param, paramValue): if isinstance(paramValue, str): layer = gvsig.currentProject().getTable(paramValue) paramValue = self.__createSextanteTable(layer()) else: paramValue = self.__createSextanteTable(paramValue()) return paramValue
def newLayer(layer, path, geometryType): #path = "C:/gvsig/test03.shp" CRS= gvsig.currentProject().getProjectionCode() schema = gvsig.createSchema(layer.getSchema()) output = gvsig.createShape( schema, path, CRS=CRS, geometryType = geometryType ) gvsig.currentView().addLayer(output) return output
def loadShapeFileFalse(shpFile, CRS='CRS:84'): try: CRS = gvsig.currentProject().getProjectionCode() except: pass layer = gvsig.loadLayer('Shape', shpFile=shpFile, CRS=CRS) gvsig.currentView().addLayer(layer) return gvsig.Layer(layer)
def loadShapeFileFalse(shpFile, CRS='CRS:84'): try: CRS = gvsig.currentProject().getProjectionCode() except: pass layer = gvsig.loadLayer('Shape', shpFile=shpFile, CRS=CRS) gvsig.currentView().addLayer(layer) return gvsig.Layer(layer)
def newLayer(layer, path, geometryType): #path = "C:/gvsig/test03.shp" CRS = gvsig.currentProject().getProjectionCode() schema = gvsig.createSchema(layer.getSchema()) output = gvsig.createShape(schema, path, CRS=CRS, geometryType=geometryType) gvsig.currentView().addLayer(output) return output
def __defineParameters(self, algorithm, kwparams): """ Define input parameters """ params = algorithm.getParameters() for i in xrange(0, params.getNumberOfParameters()): param = params.getParameter(i) if param.getParameterName() in kwparams: paramValue = kwparams[param.getParameterName()] else: paramValue = kwparams[i] #Input params: Tranform STRING to NUMERIC cond1 = (str(param) == "Numerical Value") cond2 = (str(param) == "Selection") isstr = isinstance(paramValue, str) cond4 = (str(param) == "Boolean") cond5 = (str(param) == "Table Field") print str(param), type(paramValue) if isstr: if (cond1 or cond2): paramValue = float(paramValue) elif cond4: paramValue = eval(paramValue.capitalize()) elif cond5: paramValue = int(paramValue) else: #is str pass #Vector to SEXTANTE if param.getParameterTypeName() == "Vector Layer": if isinstance(paramValue, str): layer = gvsig.currentView().getLayer(paramValue) paramValue = self.__createSextanteLayer(layer()) else: paramValue = self.__createSextanteLayer(paramValue()) #Raster to SEXTANTE elif param.getParameterTypeName() == "Raster Layer": if isinstance(paramValue, str): layer = gvsig.currentView().getLayer(paramValue) paramValue = self.__createSextanteRaster(layer) else: paramValue = self.__createSextanteRaster(paramValue) #Table to SEXTANTE elif param.getParameterTypeName() == "Table": if isinstance(paramValue, str): layer = gvsig.currentProject().getTable(paramValue) paramValue = self.__createSextanteTable(layer()) else: paramValue = self.__createSextanteTable(paramValue()) #Set parameter value param.setParameterValue(paramValue)
def loadShapeFileNew(shpFile, CRS='CRS:84', active=False, view=gvsig.currentView()): try: CRS = gvsig.currentView().getProjectionCode() except: pass layer = gvsig.loadLayer('Shape', shpFile=shpFile, CRS=CRS) if isinstance(view, str): view = gvsig.currentProject().getView(view) else: view = gvsig.currentView() view.addLayer(layer) layer.setActive(active) return gvsig.Layer(layer)
def main(*args): project = gvsig.currentProject() #DefaultProject table1 = project.getView(u"Untitled").getLayer("puntos_norep") table2 = project.getDocument("tabla") field1 = "campo2" field2 = "Punto" #data = [{'idfield':"Tipo", 'idname': id_generator()}] data = [{ 'idfield': "Tipo", 'idname': id_generator() }, { 'idfield': "Latitud", 'idname': id_generator() }] processImportFields(table1, field1, table2, field2, data)
def main(*args): currentProject = gvsig.currentProject() # Proyecto actual # Vista actual currentView = gvsig.currentView() print "Vista actual:", currentView # Capas print "Capas de la vista actual:" if currentView != None: currentViewLayers = currentView.getLayers() for currentViewLayer in currentViewLayers: print " ", currentViewLayer # Capa actual currentLayer = gvsig.currentLayer() print "Sumatory of all of the areas in the polygon layer: ", currentLayer area = calcularArea(currentView, currentLayer) if area != 0: message = str( float(area)) + " Km2" # https://es.wikipedia.org/wiki/Uruguay title = unichr( 193) + "rea" # https://es.wikipedia.org/wiki/ISO/IEC_8859-1 messageType = commonsdialog.IDEA root = None commonsdialog.msgbox(message, title, messageType, root)
def __defineExtent(self, algorithm, kwparams): """ Define Analysis Extent """ if 'EXTENT' in kwparams.keys( ) and algorithm.getUserCanDefineAnalysisExtent(): frame = kwparams['EXTENT'] if isinstance(frame, str): frame = gvsig.currentView().getLayer(frame) #print ("|"+str(frame)+"||"+str(type(frame))) if isinstance(frame, str) or isinstance(frame, gvsig.View): AExtent = AnalysisExtent() print "| EXTENT from VIEW" if isinstance(frame, gvsig.View): view = frame else: view = gvsig.currentProject().getView(frame) envelope = view.getMap().getFullEnvelope() xlow = envelope.getLowerCorner().getX() ylow = envelope.getLowerCorner().getY() xup = envelope.getUpperCorner().getX() yup = envelope.getUpperCorner().getY() AExtent.setXRange(xlow, xup, False) AExtent.setYRange(ylow, yup, False) AExtent.setZRange(0, 0, False) elif isinstance(frame, DefaultFLyrRaster): print "| EXTENT from RASTER" layer = self.__createSextanteRaster(frame) AExtent = AnalysisExtent(layer) elif isinstance(frame, list): print "| EXTENT from LIST" AExtent = AnalysisExtent() xlow, ylow, zlow, xup, yup, zup = frame[0], frame[1], frame[ 2], frame[3], frame[4], frame[5] AExtent.setXRange(xlow, xup, False) AExtent.setYRange(ylow, yup, False) AExtent.setZRange(zlow, zup, False) elif isinstance(frame, gvsig.Layer): print "| EXTENT from Layer" layer = self.__createSextanteLayer(frame()) AExtent = AnalysisExtent(layer) else: raise Exception("Not Extent Define") else: print("| Not Extent: No input data") if algorithm.canDefineOutputExtentFromInput(): algorithm.adjustOutputExtent() AExtent = AnalysisExtent() print "| EXTENT from VIEW" try: envelope = gvsig.currentView().getMap().getFullEnvelope() except: raise Exception("None open View") print "| Setting AExtent: ", try: xlow = envelope.getLowerCorner().getX() ylow = envelope.getLowerCorner().getY() zlow = 0 xup = envelope.getUpperCorner().getX() yup = envelope.getUpperCorner().getY() zup = 0 print "| View: ", print xlow, ylow, xup, yup except: xlow, ylow, zlow, xup, yup, zup = 0, 0, 0, 100, 100, 0 print "| Default:", xlow, ylow, xup, yup frame = Rectangle2D.Double(xlow, ylow, xup, yup) AExtent.setXRange(xlow, xup, False) AExtent.setYRange(ylow, yup, False) AExtent.setZRange(zlow, zup, False) algorithm.setAnalysisExtent(AExtent) #Set: cellsize if 'CELLSIZE' in kwparams.keys(): AExtent.setCellSize(kwparams['CELLSIZE']) print "| New Cellsize: ", kwparams[ 'CELLSIZE'], AExtent.getCellSize() else: print "| Cellsize: ", AExtent.getCellSize() if 'CELLSIZEZ' in kwparams.keys(): AExtent.setCellSizeZ(kwparams['CELLSIZEZ']) print "| New Cellsize Z: ", kwparams[ 'CELLSIZEZ'], AExtent.getCellSizeZ() else: print "| CellsizeZ: ", AExtent.getCellSizeZ() algorithm.setAnalysisExtent(AExtent) print("| Set Extent")
def getProjectTable(table): """Get table""" return gvsig.currentProject().getTable(table)
def getProjectTable(table): """Get table""" return gvsig.currentProject().getTable(table)
def calcularArea(currentView, currentLayer): # Ejemplo de inputbox message = "Ingrese el valor del " + unichr( 225) + "rea de referencia en km2" title = "Ingreso de datos" messageType = commonsdialog.QUESTION root = None currentProject = gvsig.currentProject() # Proyecto actual # Vista actual print "Vista actual:", currentView # Capas print "Capas de la vista actual:" if currentView != None: currentViewLayers = currentView.getLayers() for currentViewLayer in currentViewLayers: print " ", currentViewLayer # Capa actual print "Sumatory of all of the areas in the polygon layer: ", currentLayer valorReferencia = float( commonsdialog.inputbox(message, title, messageType, root)) print "valor Referencia: ", valorReferencia area = 0 if currentView != None: if currentLayer != None: polygonsLayer = currentLayer # Creamos esquema con el tipo de geometria correspondiente schema = polygonsLayer.getSchema() newSchema = createSchema(schema) # Creamos la capa nueva con el nuevo esquema newPolygonsLayer = createShape(newSchema) newPolygonsLayer.edit() features = currentLayer.features() # Entidades message = "" if features != None: try: polygonFeatures = polygonsLayer.features() contador = 0 for polygonFeature in polygonFeatures: area += polygonFeature.geometry().area() #print("area: ",area) polygon = dict() for field in schema: #print "prueba: "+str(field.getDataTypeName()) if str(field.getDataTypeName()) != "Double": polygon[field.getName()] = polygonFeature.get( field.getName()) else: polygon[field.getName()] = long( polygonFeature.get(field.getName())) tempArea = polygonFeature.geometry().area() * 1000 if tempArea > valorReferencia: newPolygonsLayer.append(polygon) contador = contador + 1 #newfeature = self.createNewFeature(output_store, polygonFeature) #newfeature["GEOMETRY"] = polygon #output_store.insert(newfeature) area = area * 10000 # area *10000 para obtener el area en kilometros cuadrados #self.addOutputText("OutputText", "Area: ") #self.addOutputNumericalValue("OutputNumerical", "area") newPolygonsLayer.commit() newPolygonsLayer.setName("Capa de poligonos mayores") currentView.addLayer(newPolygonsLayer) except: ex = sys.exc_info()[1] # Captura de excepciones message = ex.__class__.__name__ + " - " + str(ex) finally: DisposeUtils.disposeQuietly( features) # Liberacion de recursos if message != "": title = "Error" messageType = commonsdialog.FORBIDEN root = None commonsdialog.msgbox(message, title, messageType, root) else: mensajeCartel( "En la vista actual no hay seleccionada ninguna capa, debe seleccionar una capa.", "Error: se debe seleccionar una capa", commonsdialog.FORBIDEN) else: mensajeCartel("No se encuentra ninguna vista para comenzar a trabajar", "Error: sin vista", commonsdialog.FORBIDEN) return area
def __defineExtent(self, algorithm, kwparams): """ Define Analysis Extent """ if self.__defaultAE == None: if DEV_INFO: print "-------- cambiar extent" change=True else: if DEV_INFO: print "-------- no cambiar extent" change = False AExtent = self.__defaultAE changeCell = False if 'EXTENT' in kwparams.keys() and algorithm.getUserCanDefineAnalysisExtent() : changeCell = True frame = kwparams['EXTENT'] if isinstance(frame, str): frame = gvsig.currentView().getLayer(frame) #print ("|"+str(frame)+"||"+str(type(frame))) if isinstance(frame, str) or isinstance(frame, gvsig.View): AExtent = AnalysisExtent() if DEV_INFO: print "| EXTENT from VIEW" if isinstance(frame, gvsig.View): view = frame else: view = gvsig.currentProject().getView(frame) envelope = view.getMap().getFullEnvelope() xlow = envelope.getLowerCorner().getX() ylow = envelope.getLowerCorner().getY() xup = envelope.getUpperCorner().getX() yup = envelope.getUpperCorner().getY() AExtent.setXRange(xlow, xup, False) AExtent.setYRange(ylow, yup, False) AExtent.setZRange(0, 0, False) elif isinstance(frame, DefaultFLyrRaster): if DEV_INFO: print "| EXTENT from RASTER" layer = self.__createSextanteRaster(frame) AExtent = AnalysisExtent(layer) changeCell = False #AExtent.setZRange(0, 1, True) if DEV_INFO: print AExtent elif isinstance(frame, list): if DEV_INFO: print "| EXTENT from LIST" AExtent = AnalysisExtent() AExtent.setCellSize(0.49) AExtent.setCellSizeZ(0.49) xlow, ylow, zlow, xup, yup, zup = frame[0], frame[1], frame[2], frame[3], frame[4], frame[5] AExtent.setXRange(xlow, xup, True) AExtent.setYRange(ylow, yup, True) AExtent.setZRange(zlow, zup, True) elif isinstance(frame, gvsig.Layer): if DEV_INFO: print "| EXTENT from Layer" layer = self.__createSextanteLayer(frame()) AExtent = AnalysisExtent(layer) else: raise Exception("Not Extent Define") algorithm.setAnalysisExtent(AExtent) elif change == True: if DEV_INFO: print ("| Not Extent: No input data") AExtent = AnalysisExtent() changeCell = True if self.__defaultAE != None: if DEV_INFO: print "| Extent from Algorithm Layers" AExtent = self.__defaultAE else: try: if DEV_INFO: print "| Extent from View" envelope = gvsig.currentView().getMap().getFullEnvelope() except: raise Exception("None open View") if DEV_INFO: print "| Setting AExtent: ", try: #from view xlow = envelope.getLowerCorner().getX() ylow = envelope.getLowerCorner().getY() zlow = 0 xup = envelope.getUpperCorner().getX() yup = envelope.getUpperCorner().getY() zup = 0 if DEV_INFO: print "| View: ", if DEV_INFO: print xlow, ylow, xup,yup except: # default xlow, ylow, zlow, xup, yup, zup = 0,0,0,100,100,0 if DEV_INFO: print "| Default:", xlow, ylow, xup, yup frame = Rectangle2D.Double(xlow, ylow, xup, yup) AExtent.setXRange(xlow, xup, False) AExtent.setYRange(ylow, yup, False) AExtent.setZRange(zlow, zup, False) algorithm.setAnalysisExtent(AExtent) #Set: cellsize if 'CELLSIZE' in kwparams.keys(): AExtent.setCellSize(kwparams['CELLSIZE']) if DEV_INFO: print "| New Cellsize: ", kwparams['CELLSIZE'], AExtent.getCellSize() elif changeCell == True: AExtent.setCellSize(AExtent.getCellSize()) if DEV_INFO: print "| Cellsize: ", AExtent.getCellSize() if 'CELLSIZEZ' in kwparams.keys(): AExtent.setCellSizeZ(kwparams['CELLSIZEZ']) if DEV_INFO: print "| New Cellsize Z: ", kwparams['CELLSIZEZ'], AExtent.getCellSizeZ() elif changeCell == True: AExtent.setCellSize(AExtent.getCellSizeZ()) if DEV_INFO: print "| CellsizeZ: ", AExtent.getCellSizeZ() if DEV_INFO: print "Å‚Å‚ end extent: ", AExtent #AExtent.setZRange(0.0, 0.0, True) algorithm.setAnalysisExtent(AExtent) if DEV_INFO: print ("| Set Extent")
def processAlgorithm(self): """ Operacion encargada de realizar nuestro proceso. """ features=None try: """ Recogemos los parametros y creamos el conjunto de entidades asociadas a la capa de entrada. Se obtendran dos capas en la vista con el mismo tipo de datos. ** Una capa es la generada por nosotros desde el script ** La otra capa es la gestionada a traves de la Toolbox creada en output_store """ currentProject = gvsig.currentProject() # Proyecto actual # Vista actual currentView = gvsig.currentView() print "Vista actual:", currentView # Capas print "Capas de la vista actual:" if currentView != None: currentViewLayers = currentView.getLayers() for currentViewLayer in currentViewLayers: print " ", currentViewLayer params = self.getParameters() inputLayer = params.getParameterValueAsVectorLayer("LAYER") valorReferencia = int(params.getParameterValueAsDouble("AREA_VALOR")) print "valor Referencia: ", valorReferencia store = inputLayer.getFeatureStore() features = store.features() area = 0 # Creamos esquema con el tipo de geometria correspondiente schema = inputLayer.getBaseDataObject().getSchema() newSchema = createSchema(schema) # Creamos la capa nueva con el nuevo esquema newPolygonsLayer = createShape(newSchema) newPolygonsLayer.edit() features = inputLayer.getBaseDataObject().features() # Entidades message = "" if features != None: try: for feature in features: area += feature.geometry().area() polygon = dict() for field in schema: if str(field.getDataTypeName()) != "Double": polygon[field.getName()] = feature.get(field.getName()) else: polygon[field.getName()] = long(feature.get(field.getName())) tempArea = feature.geometry().area() if not inputLayer.getBaseDataObject().getProjection().isProjected(): tempArea = 10000 * tempArea if tempArea > valorReferencia: newPolygonsLayer.append(polygon) newPolygonsLayer.setName("Capa de poligonos mayores") newPolygonsLayer.commit() currentView.addLayer(newPolygonsLayer) except: ex = sys.exc_info()[1] # Captura de excepciones message = ex.__class__.__name__ + " - " + str(ex) finally: DisposeUtils.disposeQuietly(features) # Liberacion de recursos if message != "": title = "Error" messageType = commonsdialog.FORBIDEN root = None commonsdialog.msgbox(message, title, messageType, root) finally: DisposeUtils.disposeQuietly(features) print "Proceso terminado %s" % self.getCommandLineName()