Skip to content

Instantly share code, notes, and snippets.

@quommit
Last active August 29, 2015 14:10
Show Gist options
  • Save quommit/00c10298062e56316df1 to your computer and use it in GitHub Desktop.
Save quommit/00c10298062e56316df1 to your computer and use it in GitHub Desktop.
Generar Nomenclator de referencias catastrales en Redis
from argparse import ArgumentParser
import os
from lxml import etree
import redis
EXEC_DIR = os.path.dirname(__file__)
root = "/home/josetomas/Descargas/catastro_alicante_2014"
gmdns = "{http://www.isotc211.org/2005/gmd}"
gcons = "{http://www.isotc211.org/2005/gco}"
gmlns = "{http://www.opengis.net/gml/3.2}"
cpns = "{urn:x-inspire:specification:gmlas:CadastralParcels:3.0}"
def savemun(rediscache, munid, epsgid, pubtype, pubtitle, pubdate):
key = "m:{0}:{1}".format(munid, pubtype)
t = rediscache.pipeline()
t.hset(key, "epsgid", epsgid)
t.hset(key, "pubtitle", pubtitle)
t.hset(key, "pubdate", pubdate)
t.execute()
def saveparcel(rediscache, ref, x, y, created, areaval, pubtype, munid):
key = "{0}:{1}".format(ref, pubtype)
t = rediscache.pipeline()
t.hset(key, "x", x)
t.hset(key, "y", y)
t.hset(key, "created", created)
#t.hset(key, "areaval", areaval)
t.hset(key, "munid", munid)
t.execute()
return
def cat2redis(sourcemd, source, rediscache):
context = etree.iterparse(sourcemd, events=("end",),
tag= gmdns + "MD_DataIdentification")
for event, elem in context:
publication = elem.find(
gmdns + "citation").find(
gmdns + "CI_Citation")
pubtitle = publication.find(
gmdns + "title").find(
gcons + "CharacterString").text
pubid = publication.find(
gmdns + "identifier").find(
gmdns + "RS_Identifier").find(
gmdns + "authority").find(
gmdns + "CI_Citation").find(
gmdns + "title").find(
gcons + "CharacterString").text.rsplit(".", 2)
pubtype = pubid[1]
munid = pubid[2]
pubdate = publication.find(
gmdns + "identifier").find(
gmdns + "RS_Identifier").find(
gmdns + "authority").find(
gmdns + "CI_Citation").find(
gmdns + "date").find(
gmdns + "CI_Date").find(
gmdns + "date").find(
gcons + "Date").text
epsgid = publication.find(
gmdns + "identifier").find(
gmdns + "RS_Identifier").find(
gmdns + "code").find(
gcons + "CharacterString").text
elem.clear()
savemun(rediscache, munid, epsgid, pubtype, pubtitle, pubdate)
context = etree.iterparse(source, events=("end",),
tag= gmlns + "featureMember")
count = 0
for event, elem in context:
cat = elem[0]
if cat.tag == cpns + "CadastralParcel":
ref = cat.find(cpns + "nationalCadastralReference").text
refpoint = cat.find(cpns + "referencePoint")
gmlpoint = refpoint.find(gmlns + "Point")
coord = gmlpoint.find(gmlns + "pos").text.split()
x = coord[0]
y = coord[1]
created = cat.find(cpns + "beginLifespanVersion").text
areaval = cat.find(cpns + "areaValue").text
elif cat.tag == cpns + "CadastralZoning":
ref = cat.find(cpns + "nationalCadastalZoningReference").text
refpoint = cat.find(cpns + "referencePoint")
gmlpoint = refpoint.find(gmlns + "Point")
coord = gmlpoint.find(gmlns + "pos").text.split()
x = coord[0]
y = coord[1]
created = cat.find(cpns + "beginLifespanVersion").text
areaval = "-1"
elem.clear()
saveparcel(rediscache, ref, x, y, created, areaval, pubtype, munid)
count += 1
print(("Total records written: {0}".format(count)))
return
def main():
parser = ArgumentParser()
parser.add_argument("-d", "--directory", default=EXEC_DIR,
help="Directorio en que se almacena la cartografia catastral")
args = parser.parse_args()
rediscache = redis.StrictRedis()
for dirname, subdirs, files in os.walk(args.directory):
sourcemd = None
source = None
if len(files) == 2:
for f in files:
if f.endswith(".xml"):
sourcemd = os.path.join(dirname, f)
elif f.endswith(".gml"):
source = os.path.join(dirname, f)
if sourcemd is not None and source is not None:
print(("Processing file {0}".format(source)))
cat2redis(sourcemd, source, rediscache)
rediscache.bgsave()
"""Esta utilidad lee las referencias catastrales de ficheros GML en un
directorio y las almacena como como pares referencia/centroide en Redis.
El directorio raiz debe contener 2 subdirectorios por cada municipio:
- uno para la cartografia de urbana;
- otro para la cartografia de rustica.
Esta estructura de subdirectorios se puede generar desde el directorio
raiz en que se encuentren los ficheros comprimidos de catastro, ejecutando
la siguiente sentencia en bash:
$ for f in *.zip;do unzip "$f" "-d ${f%.zip}";done
Cada subdirectorio debe contener:
- el fichero .xml de metadatos
- el fichero .gml de cartografia
Ejemplo:
$ python cat2redis -d /home/user/directorio_raiz_de_ficheros_gml
"""
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment