Ciao

On Fri, 14 Dec 2007 13:13:10 +0100
Luca Casagrande <[EMAIL PROTECTED]> wrote:

> Ciao Bud,
> 
> complimenti davvero per il lavoro.

Come hai visto era una cosa veloce..  

> Potremo svilupparlo come plugin per Qgis in modo da avere lo shape giĆ  
> caricato nella mappa..

Non so ancora niente di Qgis ma suona molto bene.  

> Se ti va posso darti una mano.

Ho alcuni problemi ancora con il backend per scrivere shape files.  Ho
solo guardato un esempio del uso di shapelib senza leggere la spec...

Che suggede e' che shpdump mi fa vedere tutto ok sembra a prima vista.
QGIS non mi fa vedere niente, e usa la scala di "degrees" assumendo
lat/long penso.  Cosi una possibilita' e' che assumendo coordinate
geografiche non fa vedere niente oppure non ho fatto i "rings" nella
seguenza giusta (ho sparato senza vedere...). 

C'e' qualcuno che ha idea che potrebbe essere spagliato?

Il codice fin ora e' allegato..

saluti
-b
#######################################################################
## =======
## cml2shp
## =======
##
## Converts a subset of content from a file in
## Cadastral Markup Language (CML) [1] to a
## shape file
##
## [1] CML spec:
## http://www.agenziaterritorio.it/servizi/comunieistituzioni/..
## ..fornitura_dati_catastali/specifica%20tecnica%20CML.doc
##
## Copyright:  Comune di Grosseto
## Author:     Bud P. Bruegger <[EMAIL PROTECTED]>
## License:    GPL (any version)
#######################################################################

import operator

import shapelib, dbflib
import elementtree.ElementTree as ET

version = "0.1 14/12/2007"
inFileName = "testdata/E202_000100.CMF"
#inFileName = "testdata/vertisola.CMF"
outFileName = "testout"


class Feature(object):
    "a simple in memory rep of a feature"
    
    def __init__(self, bordoType, mappaID, particellaID, valenza, esterconf, geom):
        self.bordoType = bordoType
        self.mappaID = mappaID
        self.particellaID = particellaID
        self.valenza = valenza
        self.esterconf = esterconf
        self.geom = geom
        #geometry is represented by a list of rings,
        #where every ring is a list of x/y tuples
        #the first ring is the main one,
        #the others are islands

    def createShp(self, name):
        "creates a shp and a dbf file, the latter based on the attDict"
        shp = shapelib.create(name, shapelib.SHPT_POLYGON)
        dbf = dbflib.create(name)
        dbf.add_field("bordoType", dbflib.FTString, 20, 0)
        dbf.add_field("mappaID", dbflib.FTString, 20, 0)
        dbf.add_field("particellaID", dbflib.FTString, 20, 0)
        dbf.add_field("valenza", dbflib.FTString, 20, 0)
        dbf.add_field("esternconf", dbflib.FTString, 20, 0)
        return shp, dbf

    def writeToShp(self, shpFile, dbfFile):
        "writes out feature to a shape file"
        print self.geom
        shpObj = shapelib.SHPObject(shapelib.SHPT_POLYGON, -1, self.geom)
        #shpObj = shapelib.SHPObject(shapelib.SHPT_POLYGON, 1, [[(10,10), (10,20), (20,20), (10,10)],])
        shpFile.write_object(-1, shpObj)
        

#-------------------------------------
def parseFabbriPart(codbo):
    "parses a codbo of a fabbricato or particella"
    bordoType = "PARTICELLA"
    particellaID = codbo
    if codbo[-1] == '+':
        bordoType = "FABBRICATO"
        particellaID = codbo[:-1]
    return bordoType, particellaID
        
def codboParse(codbo, mapName):
    "parses a codbo"
    #returns: bordoType, particellaID
    codbo = codbo.strip()
    mappaID = mapName
    if codbo == mapName:
        bordoType = "MAPPA"
        particellaID = ""
    elif codbo == "STRADA":
        bordoType = "STRADA"
        particellaID = ""
    elif codbo == "ACQUA":
        bordoType = "ACQUA"
        particellaID = ""
    else:
        bordoType, particellaID = parseFabbriPart(codbo)
    return bordoType, particellaID

def vertConsume(nVerts, vertList):
    return vertList[:nVerts], vertList[nVerts:]

def coordParse(nVert, vertIsolaList, coordStr):
    noIsolaVerts = reduce(operator.add, vertIsolaList, 0)
    vertexList = [map(float, subStr.split(',')) for subStr in coordStr.split()]
    vertexList = map(tuple, vertexList)
    geom = []
    mainRing, remainingVerts = vertConsume((nVert - noIsolaVerts), vertexList)
    geom.append(mainRing)
    for noIslandVerts in vertIsolaList:
        islandRing, remainingVerts = vertConsume(noIslandVerts, remainingVerts)
        geom.append(islandRing)
    return geom

#-- main parsing ---------------------
mapTree = ET.parse(inFileName)
mapRoot = mapTree.getroot()
mapID = mapRoot.find("INFOMAPPA").get("nome")
featureList = []

#-- process BORDO elements ------------------
for border in mapRoot.getiterator("BORDO"):
    bordoType, particellaID = codboParse(border.get("codbo"), mapID)
    valenza = border.get("valenza")
    esterconf = border.get("esterconf")
    gbordo = border.find("GBORDO")
    #nIsole = gbordo.get("n.isole")
    nVert = int(gbordo.get("n.vert"))
    vertIsolaList = map(int, [i.text for i in gbordo.getiterator("VERTISOLA")])
    coordStr = gbordo.find("COORD").text
    geom = coordParse(nVert, vertIsolaList, coordStr)
    featureList.append(Feature(bordoType, mapID, particellaID,
                               valenza, esterconf, geom))

shpf, dbff = featureList[0].createShp(outFileName)
for feature in featureList:
    feature.writeToShp(shpf, dbff)
shpf.close()
dbff.close()
del shpf
del dbff
_______________________________________________
Prenota la tua maglietta GFOSS.it:
http://wiki.gfoss.it/index.php/Gadgets
Iscriviti all'associazione GFOSS.it: http://www.gfoss.it/drupal/iscrizione
[email protected]
http://www.faunalia.com/cgi-bin/mailman/listinfo/gfoss
Questa e' una lista di discussione pubblica aperta a tutti. 
I messaggi di questa lista non rispecchiano necessariamente
le posizioni dell'Associazione GFOSS.it.

Rispondere a