from gvsig import * import commonsdialog import os, sys import gvpy def main(): """Creating basic metadata text files and exporting view's layers""" #Autor: Patricio Soriano #Dependencies: Import gvpy library. More information in www.masquesig.com #input: Esri Shapes Files in a view #output: Text files with basic metadata information and, optionaly, the copies of the shape files layers global save_path global view #Ask path to save the files folder_results = commonsdialog.openFolderDialog("Select a folder") save_path = str(folder_results[0]) crs = currentView().getProjectionCode() # Checking view and layers view = currentView() if not view: msg = ("There is not any view active.","Error", 1) print "Error. There is not any view active." return if not len(view.getLayers() )>0: print "Error. There are not layers in the view." return #Copy option dialog message = "Do you want to make a copy of the shapefiles layers?" title = "Option" optionType = commonsdialog.YES_NO messageType = commonsdialog.QUESTION option = commonsdialog.confirmDialog(message, title, optionType,messageType) if option == 0: # Option Yes. Metadata files and copies. emptyDirectory() createMetadata() createLayersCopy() else: emptyDirectory() createMetadata() # Option No. Only metadata files # function emptyDirectory # Debería repasar la carpeta por si tiene datos y avisar def emptyDirectory(): if len(os.listdir(save_path)) > 0: print "Con datos" commonsdialog.msgbox("The select folder contains some files. Please select a empty directory and try again.", "Error",commonsdialog.FORBIDEN) sys.exit() # Function createMetadata def createMetadata(): for layer in currentView().getLayers(): metata_file_name = save_path+'/'+layer.name+".txt" # Creating metadata text file file = open(metata_file_name, "w") # Adding layer information file.write("Layer name: "+layer.name+"\n") file.write("Geometry: "+layer.getTypeVectorLayer().name+"\n") file.write("Layer SRS: "+layer.getProjectionCode()+"\n") file.write("Boundary box: "+str(layer.getFullEnvelope())+"\n") file.write("Feature Count: "+str(layer.features().getCount())+"\n") layer_schema = layer.getSchema() layer_attributes = layer_schema.getAttrNames() file.write("Schema\n") file.write("NAME\tData Type Name\tPrecision\n") for attribute_name in layer_attributes: attribute = layer_schema.get(attribute_name) # Adding schema attributes information file.write(attribute.name+"\t"+attribute.dataTypeName+"\t"+str(attribute.precision)+"\n") file.close() # Function createLayersCopy def createLayersCopy(): for layer in view.getLayers(): shape_path = str(save_path+'/'+layer.name+'.shp') gvpy.runalg("exportvector",LAYER=layer,PATH=shape_path)