Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Script for updating layers. #25

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@ Scripts

A collection of bash script usefull for common tasks like tomcat startup or watchdog.

See the wiki for more info on how to install and use them.
See the wiki for more info on how to install and use them.

COMMAND for updating layers
---------------------------
```python3 update_xml_file.py 'http://localhost:8080/geoserver/gwc/rest/layers/' USERNAME PASSWORD ./layers.txt ./tile_layer_template.xml.txt```

System Argument needs to pass: (USERNAME, PASSWORD, LAYERS FILE, XML FILE)
1 change: 1 addition & 0 deletions utils/update_layers/layers.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["nurc:Pk50095","spearfish","sf:restricted","nurc:Img_Sample","tasmania","sf:archsites","tiger-ny","topp:states","topp:tasmania_state_boundaries","nurc:Arc_Sample","sf:bugsites","tiger:giant_polygon","topp:tasmania_water_bodies","tiger:poly_landmarks","topp:tasmania_cities","sf:sfdem","tiger:tiger_roads","nurc:mosaic","tiger:poi","topp:tasmania_roads","sf:streams","sf:roads"]
2 changes: 2 additions & 0 deletions utils/update_layers/requriments.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
requests==2.27.1
xmltodict==0.12.0
39 changes: 39 additions & 0 deletions utils/update_layers/tile_layer_template.xml.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<GeoServerLayer>
<id>{{layer_id}}</id>
<enabled>true</enabled>
<inMemoryCached>true</inMemoryCached>
<name>{{layer_name}}</name>
<mimeFormats>
<string>image/png</string>
<string>image/jpeg</string>
<string>image/png8</string>
</mimeFormats>
<gridSubsets>
<gridSubset>
<gridSetName>EPSG:4326</gridSetName>
<extent>
<coords>
<double>7.475217105133489</double>
<double>35.21695657422812</double>
<double>19.380075671969546</double>
<double>45.893769848344476</double>
</coords>
</extent>
</gridSubset>
</gridSubsets>
<metaWidthHeight>
<int>4</int>
<int>4</int>
</metaWidthHeight>
<expireCache>0</expireCache>
<expireClients>0</expireClients>
<parameterFilters>
<styleParameterFilter>
<key>STYLES</key>
<defaultValue></defaultValue>
</styleParameterFilter>
</parameterFilters>
<gutter>0</gutter>
<cacheWarningSkips/>
</GeoServerLayer>
78 changes: 78 additions & 0 deletions utils/update_layers/update_xml_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""
Use this script

COMMAND:
python3 update_xml_file.py 'http://localhost:8080/geoserver/gwc/rest/layers/' USERNAME PASSWORD ./layers.txt ./tile_layer_template.xml.txt

System Argument needs to pass:
USERNAME
PASSWORD
LAYERS FILE
XML FILE

"""


import sys
import ast
import json
import requests
import xmltodict
from requests.auth import HTTPBasicAuth


if len(sys.argv) > 6 or len(sys.argv) < 6:
raise Exception("Expected 6 arguments are not given.")

URL = sys.argv[1]
username = sys.argv[2]
password = sys.argv[3]
layer_file = sys.argv[4]
xml_file = sys.argv[5]

if not (username and password and layer_file and xml_file):
raise Exception(
"System Argument needs to pass: (USERNAME, PASSWORD, LAYERS FILE, XML FILE)"
)
elif ".xml" not in xml_file:
raise Exception("Please pass xml file here.")
elif ".txt" not in layer_file:
raise Exception("Please pass txt file for layers.")
elif "layers" not in URL:
raise Exception("Please pass correct layer URL.")

f = open(layer_file, "r")
layers = f.read()
layers_list = ast.literal_eval(layers)

for layer_name in layers_list:
layerName = layer_name
response = requests.get(
URL + layerName + ".xml", auth=HTTPBasicAuth(username, password)
)

if response.status_code == 401:
raise Exception("Incorrect USERNAME or PASSWORD")

xpars = xmltodict.parse(response.text)

layerId = xpars["GeoServerLayer"]["id"]

f = open(xml_file, "r")
data = f.read()

file = xmltodict.parse(data)
file["GeoServerLayer"]["id"] = layerId
file["GeoServerLayer"]["name"] = layerName

headers = {"Content-Type": "application/json"}

response = requests.request(
"PUT",
URL + layerName,
headers=headers,
data=json.dumps(file),
auth=HTTPBasicAuth(username, password),
)

print(response.text)