Skip to content

Commit

Permalink
Ilimeta algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
pka committed Jun 19, 2012
1 parent 361dc91 commit 16c93cb
Show file tree
Hide file tree
Showing 4 changed files with 312 additions and 6 deletions.
16 changes: 16 additions & 0 deletions ogrprocessing/IliUtils.py
Expand Up @@ -29,6 +29,22 @@ def runJava(jar, args, progress):
SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines)
IliUtils.consoleOutput = loglines

@staticmethod
def runShellCmd(args, progress):
loglines = []
loglines.append("Ili execution console output")
if SextanteUtils.isWindows():
command = ["cmd.exe", "/C ",] + args
else:
command = args
SextanteLog.addToLog(SextanteLog.LOG_INFO, ''.join(['%s ' % c for c in command]))
fused_command = ''.join(['"%s" ' % c for c in command])
proc = subprocess.Popen(fused_command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE,stderr=subprocess.STDOUT, universal_newlines=False).stdout
for line in iter(proc.readline, ""):
loglines.append(line)
SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines)
IliUtils.consoleOutput = loglines

@staticmethod
def getConsoleOutput():
return IliUtils.consoleOutput
8 changes: 4 additions & 4 deletions ogrprocessing/ili2pg.py
Expand Up @@ -31,7 +31,7 @@ def defineCharacteristics(self):
self.addParameter(ParameterString(self.DB, "DB", "zplnaenikon"))
self.addParameter(ParameterString(self.ILI, "ILI", "/home/pi/Dropbox/Projects/geosummit/workshop/NP_73_CH_de_ili2.ili"))

self.addOutput(OutputHTML(self.OUTPUT, "Ili2Pg result"))
#self.addOutput(OutputHTML(self.OUTPUT, "Ili2Pg result"))


def processAlgorithm(self, progress):
Expand All @@ -41,7 +41,7 @@ def processAlgorithm(self, progress):
db = self.getParameterValue(self.DB)
ili = self.getParameterValue(self.ILI)

output = self.getOutputValue(self.OUTPUT)
#output = self.getOutputValue(self.OUTPUT)

IliUtils.runJava( '/home/pi/apps/ili2pg-1.4.0/ili2pg.jar', ['-schemaimport', '-dbdatabase', db, '-dbusr', 'pi', "-models", ili], progress )

Expand All @@ -66,7 +66,7 @@ def defineCharacteristics(self):
self.addParameter(ParameterString(self.ILI, "ILI", "/home/pi/Dropbox/Projects/geosummit/workshop/NP_73_CH_de_ili2.ili"))
self.addParameter(ParameterString(self.XTF, "XTF", "/tmp/out.xtf"))

self.addOutput(OutputHTML(self.OUTPUT, "Ili2Pg result"))
#self.addOutput(OutputHTML(self.OUTPUT, "Ili2Pg result"))


def processAlgorithm(self, progress):
Expand All @@ -77,6 +77,6 @@ def processAlgorithm(self, progress):
ili = self.getParameterValue(self.ILI)
xtf = self.getParameterValue(self.XTF)

output = self.getOutputValue(self.OUTPUT)
#output = self.getOutputValue(self.OUTPUT)

IliUtils.runJava( '/home/pi/apps/ili2pg-1.4.0/ili2pg.jar', ['-export', '-dbdatabase', db, '-dbusr', 'pi', "-models", ili, xtf], progress )
290 changes: 290 additions & 0 deletions ogrprocessing/ilismeta.py
@@ -0,0 +1,290 @@
from sextante.core.GeoAlgorithm import GeoAlgorithm
from sextante.outputs.OutputHTML import OutputHTML
from sextante.parameters.ParameterVector import ParameterVector
from sextante.parameters.ParameterString import ParameterString
from sextante.parameters.ParameterBoolean import ParameterBoolean
from sextante.core.Sextante import Sextante
from sextante.core.SextanteLog import SextanteLog
from sextante.core.QGisLayers import QGisLayers
from ogralgorithm import OgrAlgorithm
from qgis.core import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from IliUtils import *
from xml.etree import ElementTree
from xml.dom import minidom
import string
import sys


def prettify(rawxml, indent=" "):
"""Return a pretty-printed XML string
"""
reparsed = minidom.parseString(rawxml)
return reparsed.toprettyxml(indent)

def extract_enums_asgml(fn):
"""Extract Interlis Enumerations as GML
"""
tree = ElementTree.parse(fn)
#Extract default namespace from root e.g. {http://www.interlis.ch/INTERLIS2.3}TRANSFER
#ns = tree.getroot().tag
ns = "http://www.interlis.ch/INTERLIS2.3"

models = tree.findall("{%s}DATASECTION/{%s}IlisMeta07.ModelData" % (ns, ns))
if models != None:
#GML output
gml = ElementTree.Element('FeatureCollection')
gml.set('xmlns', 'http://ogr.maptools.org/')
gml.set('xmlns:gml', 'http://www.opengis.net/gml')
#<ogr:FeatureCollection
# xmlns:ogr="http://ogr.maptools.org/"
# xmlns:gml="http://www.opengis.net/gml">

for model in models:
enumNodes = model.findall("{%s}IlisMeta07.ModelData.EnumNode" % ns)

if enumNodes != None:
#Collect parent enums
parent_nodes = set()
for enumNode in enumNodes:
parent = enumNode.find("{%s}ParentNode" % ns)
if parent != None:
parent_nodes.add(parent.get("REF"))

curEnum = None
curEnumName = None
enumIdx = 0
idx = None
for enumNode in enumNodes:
parent = enumNode.find("{%s}ParentNode" % ns)
if parent == None:
curEnum = enumNode
#enum name should not be longer than 63 chars, which is PG default name limit
#Nutzungsplanung.Nutzungsplanung.Grundnutzung_Zonenflaeche.Herkunft.TYPE -> enumXX_herkunft
enumTypeName = enumNode.find("{%s}EnumType" % ns).get('REF')
enumTypeName = string.replace(enumTypeName, '.TYPE', '')
enumTypeName = string.rsplit(enumTypeName, '.', maxsplit=1)[-1]
curEnumName = "enum%d_%s" % (enumIdx, enumTypeName)
enumIdx = enumIdx + 1
#curEnumName = curEnum.get("TID")
#Remove trailing .TOP or .TYPE
#curEnumName = string.replace(curEnumName, '.TOP', '')
#curEnumName = string.replace(curEnumName, '.TYPE', '')
#curEnumName = string.replace(curEnumName, '.', '__')
idx = 0
else:
if enumNode.get("TID") not in parent_nodes:
# <gml:featureMember>
# <ogr:Grundzonen__GrundZonenCode__ZonenArt>
# <ogr:value>Dorfkernzone</ogr:value><ogr:id>0</ogr:id>
# </ogr:Grundzonen__GrundZonenCode__ZonenArt>
# </gml:featureMember>
featureMember = ElementTree.SubElement(gml, "gml:featureMember")
feat = ElementTree.SubElement(featureMember, curEnumName)
id = ElementTree.SubElement(feat, "id")
id.text = str(idx)
idx = idx + 1
enum = ElementTree.SubElement(feat, "enum")
enum.text = string.replace(enumNode.get("TID"), curEnum.get("TID")+'.', '')
enumtxt = ElementTree.SubElement(feat, "enumtxt")
enumtxt.text = enum.text
return ElementTree.tostring(gml, 'utf-8')

class Ili2Imd(OgrAlgorithm):

#constants used to refer to parameters and outputs.
#They will be used when calling the algorithm from another algorithm,
#or when calling SEXTANTE from the QGIS console.
OUTPUT = "OUTPUT"
ILI = "ILI"

def defineCharacteristics(self):
self.name = "ili to XML metamodel"
self.group = "Interlis"

#It is a mandatory (not optional) one, hence the False argument
#self.addParameter(ParameterVector(self.INPUT_LAYER, "Input layer", ParameterVector.VECTOR_TYPE_ANY, False))
self.addParameter(ParameterString(self.ILI, "ILI", "/home/pi/Dropbox/Projects/geosummit/workshop/NP_73_CH_de_ili2.ili"))

#self.addOutput(OutputHTML(self.OUTPUT, "ili2c result"))


def processAlgorithm(self, progress):
'''Here is where the processing itself takes place'''

#input = self.getParameterValue(self.INPUT_LAYER)
ili = self.getParameterValue(self.ILI)

#output = self.getOutputValue(self.OUTPUT)

#ili2c USAGE
# ili2c [Options] file1.ili file2.ili ...
#
#OPTIONS
#
#--no-auto don't look automatically after required models.
#-o0 Generate no output (default).
#-o1 Generate INTERLIS-1 output.
#-o2 Generate INTERLIS-2 output.
#-oXSD Generate an XML-Schema.
#-oFMT Generate an INTERLIS-1 Format.
#-oIMD Generate Model as IlisMeta INTERLIS-Transfer (XTF).
#-oIOM (deprecated) Generate Model as INTERLIS-Transfer (XTF).
#--out file/dir file or folder for output.
#--ilidirs %ILI_DIR;http://models.interlis.ch/;%JAR_DIR list of directories with ili-files.
#--proxy host proxy server to access model repositories.
#--proxyPort port proxy port to access model repositories.
#--with-predefined Include the predefined MODEL INTERLIS in
# the output. Usually, this is omitted.
#--without-warnings Report only errors, no warnings. Usually,
# warnings are generated as well.
#--trace Display detailed trace messages.
#--quiet Suppress info messages.
#-h|--help Display this help text.
#-u|--usage Display short information about usage.
#-v|--version Display the version of ili2c.

#TODO: write in temp file and return as string
IliUtils.runJava( '/home/pi/apps/ili2c-4.4.4/ili2c.jar', ["-oIMD", "--out", string.replace(ili, '.ili', '') + '.imd', ili], progress )


class EnumsAsGML(OgrAlgorithm):

#constants used to refer to parameters and outputs.
#They will be used when calling the algorithm from another algorithm,
#or when calling SEXTANTE from the QGIS console.
OUTPUT = "OUTPUT"
IMD= "IMD"
GML= "GML"

def defineCharacteristics(self):
self.name = "Export Enums to GML"
self.group = "Interlis"

#It is a mandatory (not optional) one, hence the False argument
#self.addParameter(ParameterVector(self.INPUT_LAYER, "Input layer", ParameterVector.VECTOR_TYPE_ANY, False))
self.addParameter(ParameterString(self.IMD, "IMD", "/home/pi/Dropbox/Projects/geosummit/workshop/NP_73_CH_de_ili2.imd"))
self.addParameter(ParameterString(self.GML, "GML", "/tmp/enums.gml"))

#self.addOutput(OutputHTML(self.OUTPUT, "EnumsAsGML result"))


def processAlgorithm(self, progress):
'''Here is where the processing itself takes place'''

#input = self.getParameterValue(self.INPUT_LAYER)
imd = self.getParameterValue(self.IMD)
gmlout = self.getParameterValue(self.GML)

#output = self.getOutputValue(self.OUTPUT)

gml = extract_enums_asgml(imd)

f = open(gmlout, "w")
f.write(prettify(gml))
f.close()


class ImportGML(OgrAlgorithm):

#constants used to refer to parameters and outputs.
#They will be used when calling the algorithm from another algorithm,
#or when calling SEXTANTE from the QGIS console.
OUTPUT = "OUTPUT"
DB = "DB"
GML= "GML"

def defineCharacteristics(self):
self.name = "Import Enums from GML"
self.group = "Interlis"

#It is a mandatory (not optional) one, hence the False argument
#self.addParameter(ParameterVector(self.INPUT_LAYER, "Input layer", ParameterVector.VECTOR_TYPE_ANY, False))
self.addParameter(ParameterString(self.GML, "GML", "/tmp/enums.gml"))
self.addParameter(ParameterString(self.DB, "DB", "zplnaenikon"))

#self.addOutput(OutputHTML(self.OUTPUT, "EnumsAsGML result"))


def processAlgorithm(self, progress):
'''Here is where the processing itself takes place'''

#input = self.getParameterValue(self.INPUT_LAYER)
gml = self.getParameterValue(self.GML)
db = self.getParameterValue(self.DB)

#output = self.getOutputValue(self.OUTPUT)

IliUtils.runShellCmd(["ogr2ogr", "-f", "PostgreSQL", "-a_srs", "EPSG:21781", "PG:\"dbname='%s'\"" % db, gml], progress)


class IliEnumsToPg(OgrAlgorithm):

#constants used to refer to parameters and outputs.
#They will be used when calling the algorithm from another algorithm,
#or when calling SEXTANTE from the QGIS console.
OUTPUT = "OUTPUT"
ILI = "ILI"
DB = "DB"

def defineCharacteristics(self):
self.name = "Ili Enums to PG"
self.group = "Interlis"

#It is a mandatory (not optional) one, hence the False argument
#self.addParameter(ParameterVector(self.INPUT_LAYER, "Input layer", ParameterVector.VECTOR_TYPE_ANY, False))
self.addParameter(ParameterString(self.ILI, "ILI", "/home/pi/Dropbox/Projects/geosummit/workshop/NP_73_CH_de_ili2.ili"))
self.addParameter(ParameterString(self.DB, "DB", "zplnaenikon"))

#self.addOutput(OutputHTML(self.OUTPUT, "EnumsAsGML result"))


def processAlgorithm(self, progress):
'''Here is where the processing itself takes place'''

#input = self.getParameterValue(self.INPUT_LAYER)
ili = self.getParameterValue(self.ILI)
gml = '/tmp/enums.gml'
imd = '/tmp/model.imd'
db = self.getParameterValue(self.DB)

#output = self.getOutputValue(self.OUTPUT)

IliUtils.runJava( '/home/pi/apps/ili2c-4.4.4/ili2c.jar', ["-oIMD", "--out", imd, ili], progress )
gmlstr = extract_enums_asgml(imd)
f = open(gml, "w")
f.write(prettify(gmlstr))
f.close()
IliUtils.runShellCmd(["ogr2ogr", "-f", "PostgreSQL", "-a_srs", "EPSG:21781", "PG:\"dbname='%s'\"" % db, gml], progress)


class CreatePGDb(OgrAlgorithm):

#constants used to refer to parameters and outputs.
#They will be used when calling the algorithm from another algorithm,
#or when calling SEXTANTE from the QGIS console.
OUTPUT = "OUTPUT"
DB = "DB"

def defineCharacteristics(self):
self.name = "Create PostGIS databse"
self.group = "Miscellaneous"

#It is a mandatory (not optional) one, hence the False argument
#self.addParameter(ParameterVector(self.INPUT_LAYER, "Input layer", ParameterVector.VECTOR_TYPE_ANY, False))
self.addParameter(ParameterString(self.DB, "DB", "zplnaenikon"))

#self.addOutput(OutputHTML(self.OUTPUT, "EnumsAsGML result"))


def processAlgorithm(self, progress):
'''Here is where the processing itself takes place'''

#input = self.getParameterValue(self.INPUT_LAYER)
template = 'template_postgis'
db = self.getParameterValue(self.DB)

#output = self.getOutputValue(self.OUTPUT)

IliUtils.runShellCmd(["createdb", "--template=%s" % template, db], progress)
4 changes: 2 additions & 2 deletions ogrprocessing/ogralgorithmprovider.py
Expand Up @@ -5,7 +5,7 @@
from ogr2ogr import Ogr2Ogr, Ogr2OgrVrt
from ogrsql import OgrSql
from ili2pg import Ili2Pg, Pg2Ili
#from interlis.ilismeta_enums import prettify
from ilismeta import Ili2Imd, EnumsAsGML, ImportGML, IliEnumsToPg, CreatePGDb


class OgrAlgorithmProvider(AlgorithmProvider):
Expand All @@ -14,7 +14,7 @@ class OgrAlgorithmProvider(AlgorithmProvider):

def __init__(self):
AlgorithmProvider.__init__(self)
self.alglist = [OgrInfo(), Ogr2Vrt(), Ogr2Ogr(), Ogr2OgrVrt(), OgrSql(), Ili2Pg(), Pg2Ili()]
self.alglist = [OgrInfo(), Ogr2Vrt(), Ogr2Ogr(), Ogr2OgrVrt(), OgrSql(), Ili2Pg(), Pg2Ili(), Ili2Imd(), EnumsAsGML(), ImportGML(), IliEnumsToPg(), CreatePGDb()]
for alg in self.alglist:
alg.provider = self

Expand Down

0 comments on commit 16c93cb

Please sign in to comment.