drought_backup/gs.py

134 lines
4.2 KiB
Python

import os
import requests
class GeoServer(object):
def __init__(self, url, name, password, workspace):
self.url = url
self.credential = (name, password)
self.workspace = workspace
self.geoserver_data_directory = '/var/lib/tomcat8/webapps/geoserver/data'
def create_and_publish_geotiff(self, file_path):
if not file_path:
return
name = os.path.splitext(os.path.basename(file_path))[0]
response = self.create_coveragestorage(name, file_path)
response.raise_for_status()
response = self.publish_geotiff(name)
response.raise_for_status()
return name
def create_and_publish_shp(self, file_path):
if not file_path:
return
name = os.path.splitext(os.path.basename(file_path))[0]
response = self.create_datastorage(name, file_path)
response.raise_for_status()
response = self.publish_shp(name, file_path)
response.raise_for_status()
return name
def publish_shp(self, name, file_path):
full_file_path = os.path.join(self.geoserver_data_directory, file_path)
print(full_file_path)
payload = 'file:%s' % (full_file_path,)
headers = {
'content-type': 'text/plain'
}
request_url = '%s/rest/workspaces/%s/datastores/%s/external.shp' % (self.url, self.workspace, name)
r = requests.put(
request_url,
data=payload,
headers=headers,
auth=self.credential
)
print(r.status_code)
print(r.text)
return r
def create_datastorage(self, name, file_path):
payload = {
'dataStore': {
'name': name,
'connectionParameters': {
'entry': [
{'@key': 'url', '$': 'file:%s' % (file_path,)}
]
}
}
}
headers = {
'content-type': 'application/json'
}
create_datastorage_url = '%s/rest/workspaces/%s/datastores' % (self.url, self.workspace)
r = requests.post(create_datastorage_url,
json=payload,
headers=headers,
auth=self.credential)
return r
def create_coveragestorage(self, name, file_path):
payload = {
'coverageStore': {
'name': name,
'workspace': self.workspace,
'type': 'GeoTIFF',
'enabled': 'true',
'url': 'file:%s' % (file_path,)
}
}
headers = {
'content-type': 'application/json'
}
create_coveragestore_url = '%s/rest/workspaces/%s/coveragestores' % (self.url, self.workspace)
r = requests.post(create_coveragestore_url,
json=payload,
headers=headers,
auth=self.credential)
return r
def publish_geotiff(self, name):
payload = {
'coverage': {
'title': name,
'name': name,
'nativeName': name,
'srs': 'EPSG:4326',
},
}
headers = {
'content-type': 'application/json'
}
publish_geotiff_url = '%s/rest/workspaces/%s/coveragestores/%s/coverages' % (self.url, self.workspace, name)
r = requests.post(publish_geotiff_url,
json=payload,
headers=headers,
auth=self.credential)
return r
if __name__ == '__main__':
# the geotiff file should be named starts with bj_ , such as bj_sample.tiff,
gs = GeoServer('http://210.77.68.250:8080/geoserver', 'admin', 'geoserver', 'baoji')
for y in range(1979, 2016):
for m in range(1, 13):
file_name = "%s-%s-temp.tif" % (y, m)
print(file_name)
name = gs.create_and_publish_geotiff('baoji/temp/%s' % (file_name))
print('coverage storage name :' + name)
print('layer name :' + name)
# name = gs.create_and_publish_shp('baoji/sample/baoji_grid.shp')
# print('data storage name:' + name)
# print('layer name:' + name)