Skip to content

Commit

Permalink
Modificado para que si son todo poligonos exporte a shape.
Browse files Browse the repository at this point in the history
  • Loading branch information
jjdelcerro authored and jjdelcerro committed Jun 4, 2018
1 parent 4c466cb commit 58b9133
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 33 deletions.
4 changes: 2 additions & 2 deletions actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def selfRegister():
i18n = ToolsLocator.getI18nManager()
moduleId = "sigpac2csv"
actionName = "tools-sigpac-sigpac2csv"
tooltip_key = i18n.getTranslation("_Convert_SIGPAC_XML_to_CSV")
menu_entry = "tools/SIGPAC/_Convert_XML_to_CSV"
tooltip_key = i18n.getTranslation("_Convert_SIGPAC_XML_to_gvSIG_format")
menu_entry = "tools/SIGPAC/_Convert_XML_to_gvSIG"

extension = SIGPAC2CSVExtension()

Expand Down
8 changes: 4 additions & 4 deletions i18n/text.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
#Wed May 30 13:11:15 CEST 2018
_Convert_SIGPAC_XML_to_CSV=Convertir SIGPAC XML a CSV
#Mon Jun 04 14:02:08 CEST 2018
_File_%s_creaded_XnlXDo_you_want_to_load_it_in_the_current_viewXquestionX=Creado fichero '%s'.\\n\u00BF Desea cargarlo en la vista actual ?
_The_CVS_file_%s_already_existsXnlXDo_you_want_to_overwrite_itXquestionX=Ya existe el fichero CVS (%s).\\n\u00BF Desea sobreescribirlo ?
_Convert_XML_to_CSV=Convertir XML a CSV
_Convert_XML_to_gvSIG=Convertir XML a gvSIG
_Convert_SIGPAC_XML_to_gvSIG_format=Convertir SIGPAC XML a un formato de gvSIG
_The_file_%s_already_existsXnlXDo_you_want_to_overwrite_itXquestionX=Ya existe el fichero (%s).\\n\u00BF Desea sobreescribirlo ?
_Select_the_SIGPAC_XML_file=Seleccione el fichero XML del SIGPAC
8 changes: 4 additions & 4 deletions i18n/text_en.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
#Wed May 30 13:11:21 CEST 2018
_Convert_SIGPAC_XML_to_CSV=Convert SIGPAC XML to CSV
#Mon Jun 04 14:01:59 CEST 2018
_File_%s_creaded_XnlXDo_you_want_to_load_it_in_the_current_viewXquestionX=File %s creaded\\nDo you want to load it in the current view?
_The_CVS_file_%s_already_existsXnlXDo_you_want_to_overwrite_itXquestionX=The CVS file %s already_exists\\nDo you want to overwrite it?
_Convert_XML_to_CSV=Convert XML to CSV
_Convert_XML_to_gvSIG=Convert XML to gvSIG
_Convert_SIGPAC_XML_to_gvSIG_format=Convert SIGPAC XML to gvSIG format
_The_file_%s_already_existsXnlXDo_you_want_to_overwrite_itXquestionX=The file %s already_exists\\nDo you want to overwrite it?
_Select_the_SIGPAC_XML_file=Select the SIGPAC XML file
155 changes: 132 additions & 23 deletions sigpac2csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,29 @@
from java.io import File
from org.gvsig.andami import PluginsLocator

from org.gvsig.fmap.geom import GeometryLocator
from org.gvsig.fmap.geom.primitive import Polygon

from gvsig import createFeatureType

from gvsig.geom import D2
from gvsig.geom import POLYGON

from gvsig import createShape


from org.gvsig.fmap.dal.feature import FeatureStore

def null2empty(n):
if n==None:
return ""
return n

def null2zero(n):
if n==None:
return 0
return n

def outint(f,s, last=False):
if s==None or s.strip()=="":
f.write("0")
Expand All @@ -40,26 +63,7 @@ def outstr(f,s, last=False):
if not last:
f.write("; ")

def sigpac2csv():
i18n = ToolsLocator.getI18nManager()
initPath = ToolsUtilLocator.getFileDialogChooserManager().getLastPath("OPEN_LAYER_FILE_CHOOSER_ID", None)
f = openFileDialog(
i18n.getTranslation("_Select_the_SIGPAC_XML_file"),
initialPath=initPath.getAbsolutePath()
)
if f==None or len(f)==0 or f[0]==None:
return
xmlf = f[0]
layername = os.path.splitext(os.path.basename(xmlf))[0]

outf = os.path.splitext(xmlf)[0]+".csv"
if os.path.exists(outf):
if confirmDialog(i18n.getTranslation("_The_CVS_file_%s_already_existsXnlXDo_you_want_to_overwrite_itXquestionX") % os.path.basename(outf))==NO:
return

with open(getResource(__file__, xmlf), 'r') as f:
xml = f.read()

def convert2cvs(data, outf):
out = open(outf,"w")
out.write("ID_ALE:Integer; ")
out.write("ID_EXP:Integer; ")
Expand Down Expand Up @@ -88,14 +92,13 @@ def sigpac2csv():
out.write("DN_SURFACE:Double; ")
out.write("WKT:Geometry:polygon\n")

d = xmltodic.parse(xml)
for linea in d["DECLARACION"]["LINEA_DECLARACION"]:
for linea in data["DECLARACION"]["LINEA_DECLARACION"]:
outint(out, linea["ID_ALE"])
outint(out, linea["ID_EXP"])
outstr(out, linea["EXP_COD"])
outstr(out, linea["TEX_NIF"])
outint(out, linea["ID_CROQUIS"])
outint(out, linea["ID_ALE"])
outint(out, linea["PROV"])
outint(out, linea["MUN_CAT"])
outint(out, linea["AGREGADO"])
outint(out, linea["ZONA"])
Expand All @@ -119,6 +122,112 @@ def sigpac2csv():
out.write("\n")
out.close()

def convert2shp(data, outf):
featureType = createFeatureType()

featureType.append("ID_ALE", "INTEGER")
featureType.append("ID_EXP", "INTEGER")
featureType.append("EXP_COD", "STRING", 25)
featureType.append("TEX_NIF", "STRING", 25)
featureType.append("ID_CROQUIS", "INTEGER")
featureType.append("PROV", "INTEGER")
featureType.append("MUN_CAT", "INTEGER")
featureType.append("AGREGADO", "INTEGER")
featureType.append("ZONA", "INTEGER")
featureType.append("POLIGONO", "INTEGER")
featureType.append("PARCELA", "INTEGER")
featureType.append("RECINTO", "INTEGER")
featureType.append("COD_TIP_AL", "STRING", 25)
featureType.append("USO", "STRING", 100)
featureType.append("SUPERF_DEC", "DOUBLE")
featureType.append("COEF_REG", "DOUBLE")
featureType.append("SECANO_REG", "DOUBLE")
featureType.append("ELEGIBILID", "DOUBLE")
featureType.append("FC_ALMENDR", "DOUBLE")
featureType.append("FC_ALGARRO", "DOUBLE")
featureType.append("FC_AVELLAN", "DOUBLE")
featureType.append("FC_NOGALES", "DOUBLE")
featureType.append("FC_PISTACH", "DOUBLE")
featureType.append("DN_SURFACE", "DOUBLE")
featureType.append("FC_TOTAL", "DOUBLE")
featureType.append("GEOMETRY", "GEOMETRY").setGeometryType(POLYGON, D2)

shape = createShape(featureType, outf)

store = shape.getDataStore()
store.edit() #FeatureStore.MODE_APPEND)

for linea in data["DECLARACION"]["LINEA_DECLARACION"]:
feature = store.createNewFeature()
feature.set("ID_ALE", null2zero(linea["ID_ALE"]))
feature.set("ID_EXP", null2zero(linea["ID_EXP"]))
feature.set("EXP_COD", null2empty(linea["EXP_COD"]))
feature.set("TEX_NIF", null2empty(linea["TEX_NIF"]))
feature.set("ID_CROQUIS", null2zero(linea["ID_CROQUIS"]))
feature.set("PROV", null2zero(linea["PROV"]))
feature.set("MUN_CAT", null2zero(linea["MUN_CAT"]))
feature.set("AGREGADO", null2zero(linea["AGREGADO"]))
feature.set("ZONA", null2zero(linea["ZONA"]))
feature.set("POLIGONO", null2zero(linea["POLIGONO"]))
feature.set("PARCELA", null2zero(linea["PARCELA"]))
feature.set("RECINTO", null2zero(linea["RECINTO"]))
feature.set("COD_TIP_AL", null2empty(linea["COD_TIPO_ALE"]))
feature.set("USO", null2empty(linea["USO"]))
feature.set("SUPERF_DEC", null2zero(linea["SUPERFICIE_DECLARADA"]))
feature.set("COEF_REG", null2zero(linea["COEF_REG"]))
feature.set("SECANO_REG", null2zero(linea["SECANO_REGADIO"]))
feature.set("ELEGIBILID", null2zero(linea["ELEGIBILIDAD"]))
feature.set("FC_ALMENDR", null2zero(linea["FC_ALMENDROS"]))
feature.set("FC_ALGARRO", null2zero(linea["FC_ALGARROBOS"]))
feature.set("FC_AVELLAN", null2zero(linea["FC_AVELLANOS"]))
feature.set("FC_NOGALES", null2zero(linea["FC_NOGALES"]))
feature.set("FC_PISTACH", null2zero(linea["FC_PISTACHOS"]))
feature.set("FC_TOTAL", null2zero(linea["FC_TOTAL"]))
feature.set("DN_SURFACE", null2zero(linea["DN_SURFACE"]))
feature.set("GEOMETRY", linea["WKT"])
store.insert(feature)
store.finishEditing()


def hasOnlyPoligons(data):
geometryManager = GeometryLocator.getGeometryManager()
for linea in data["DECLARACION"]["LINEA_DECLARACION"]:
geom = geometryManager.createFrom(linea["WKT"])
if not isinstance(geom, Polygon):
return False
return True

def sigpac2csv():
i18n = ToolsLocator.getI18nManager()
initPath = ToolsUtilLocator.getFileDialogChooserManager().getLastPath("OPEN_LAYER_FILE_CHOOSER_ID", None)
f = openFileDialog(
i18n.getTranslation("_Select_the_SIGPAC_XML_file"),
initialPath=initPath.getAbsolutePath()
)
if f==None or len(f)==0 or f[0]==None:
return
xmlf = f[0]
layername = os.path.splitext(os.path.basename(xmlf))[0]

with open(getResource(__file__, xmlf), 'r') as f:
xml = f.read()
data = xmltodic.parse(xml)

if hasOnlyPoligons(data) :
output = "shp"
else:
output = "csv"

outf = os.path.splitext(xmlf)[0]+"." + output
if os.path.exists(outf):
if confirmDialog(i18n.getTranslation("_The_file_%s_already_existsXnlXDo_you_want_to_overwrite_itXquestionX") % os.path.basename(outf))==NO:
return

if output == "csv":
convert2cvs(data, outf)
else:
convert2shp(data, outf)

view = currentView()
if view == None:
return
Expand Down

0 comments on commit 58b9133

Please sign in to comment.