Last active
June 27, 2018 15:02
-
-
Save leodc/ed47a5945f4511d5b5e86eb261d979e6 to your computer and use it in GitHub Desktop.
python3 renameGeoserverLayers.py data_directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import os | |
import sys | |
import shutil | |
import urllib.request | |
import json | |
import xml.etree.ElementTree as etree | |
import subprocess | |
if len(sys.argv) < 2: | |
print("Error: Necesitas especificar un directorio") | |
sys.exit() | |
GEOSERVER_WORKSPACE = os.getenv("GEOSERVER_WORKSPACE") | |
GEOSERVER_REST = os.getenv("GEOSERVER_REST") | |
GEOSERVER_USER = os.getenv("GEOSERVER_USER") | |
GEOSERVER_PASSWORD = os.getenv("GEOSERVER_PASSWORD") | |
CKAN_API = "https://datos.gob.mx/ckan-admin/api/3/action/package_show?id=" | |
DGM_API = "https://api.datos.gob.mx/v1/resources?id=" | |
rootDirectory = sys.argv[1] | |
def main(): | |
# get processed layers | |
if os.path.exists("processed_layers"): | |
processed_layers = open("processed_layers","r").read().splitlines() | |
else: | |
processed_layers = [] | |
for directory in os.listdir(rootDirectory): | |
if "." in directory or directory in processed_layers: | |
print("Omitiendo " + directory) | |
continue | |
resourceId = directory.split("__")[0].replace("_","-") | |
print("Procesando {}...".format(directory), end=" ", flush=True) | |
resourcePath = rootDirectory + directory + "/featuretype.xml" | |
tree = etree.parse(resourcePath) | |
if validCoordinates( tree.getroot().find("latLonBoundingBox") ): | |
with urllib.request.urlopen(DGM_API + resourceId) as dgm_response: | |
dgm_result = json.loads(dgm_response.read().decode("utf-8")) | |
if len(dgm_result["results"]) >= 1: | |
dgm_resource = dgm_result["results"][0] | |
if len(directory.split("__")) > 1: | |
dgm_resource["name"] = dgm_resource["name"] + " - " + directory.split("__")[1] | |
with urllib.request.urlopen(CKAN_API + dgm_resource["package-id"]) as ckan_response: | |
ckan_resource = json.loads(ckan_response.read().decode("utf-8"))["result"] | |
processFeatureType(tree, dgm_resource, ckan_resource) | |
# write new xml | |
tree.write(resourcePath, encoding="UTF-8") | |
print("Metadatos actualizados correctamente") | |
else: | |
print("No se encontro el recurso", json.dumps(dgm_result)) | |
deleteLayer(directory) | |
else: | |
print("Extensión del layer no valido") | |
deleteLayer(directory) | |
# shutil.rmtree(rootDirectory + directory) | |
print("----- Done") | |
def deleteLayer(layerName): | |
print("Deleting layer: " + layerName) | |
request = "curl -v -u {user}:{password} \ | |
-X DELETE \ | |
{geoserver_rest}/layers/{workspace}:{layer}.xml".format(user=GEOSERVER_USER, password=GEOSERVER_PASSWORD, layer=layerName, workspace=GEOSERVER_WORKSPACE, geoserver_rest=GEOSERVER_REST) | |
print(request) | |
subprocess.call( request , shell=True) | |
def validCoordinates(bounds): | |
maxx = float(bounds.find("maxx").text) | |
minx = float(bounds.find("minx").text) | |
miny = float(bounds.find("miny").text) | |
maxy = float(bounds.find("maxy").text) | |
return (-180.0000 < minx and minx < 180.0000) and (-180.0000 < maxx and maxx < 180.0000) and (-90.0000 < miny and miny < 90.0000) and (-90.0000 < maxy and maxy < 90.0000) | |
def processFeatureType(tree, dgm_resource, ckan_resource): | |
root = tree.getroot() | |
# Layer showed title | |
if dgm_resource["name"] is not None: | |
root.find("title").text = dgm_resource["name"] | |
# abstract | |
if "description" in dgm_resource: | |
if root.find("abstract") is None: | |
root.append( etree.Element("abstract") ) | |
root.find("abstract").text = dgm_resource["description"] | |
# tags | |
tags = [] | |
## organization | |
if "organization" in dgm_resource and dgm_resource["organization"] is not None: | |
tags.append(dgm_resource["organization"]) | |
if "tags" in ckan_resource: | |
for tag in ckan_resource["tags"]: | |
if tag["state"] == "active": | |
tags.append(tag["display_name"]) | |
for tag in tags: | |
keywords = root.find("keywords") | |
etree.SubElement(keywords, "string").text = tag | |
# init | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment