Skip to content

Commit 1ab51d0

Browse files
authored
Merge pull request #143 from SeaBee-no/create-geopackage-datastore
Create geopackage datastore
2 parents c079dc5 + 0a4a1fe commit 1ab51d0

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

docs/source/change_log.rst

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Change Log
33

44
``Master branch``
55
^^^^^^^^^^^^^^^^^
6+
* New method `create_gpkg_datastore`
67
* Bugfixes for `add_layer_to_layergroup` and `remove_layer_from_layergroup`
78
* New method `remove_layer_from_layergroup`
89

geo/Geoserver.py

+59
Original file line numberDiff line numberDiff line change
@@ -1822,6 +1822,65 @@ def create_shp_datastore(
18221822
else:
18231823
raise GeoserverException(r.status_code, r.content)
18241824

1825+
def create_gpkg_datastore(
1826+
self,
1827+
path: str,
1828+
store_name: Optional[str] = None,
1829+
workspace: Optional[str] = None,
1830+
file_extension: str = "gpkg",
1831+
):
1832+
"""
1833+
Create datastore for a geopackage.
1834+
1835+
Parameters
1836+
----------
1837+
path : str
1838+
Path to the geopackage file.
1839+
store_name : str, optional
1840+
Name of store to be created. If None, parses from the filename.
1841+
workspace: str, optional
1842+
Name of workspace to be used. Default: "default".
1843+
file_extension : str
1844+
1845+
Notes
1846+
-----
1847+
The layer name will be assigned according to the layer name in the geopackage.
1848+
If the layer already exist it will be updated.
1849+
"""
1850+
1851+
if path is None:
1852+
raise Exception("You must provide a full path to shapefile")
1853+
1854+
if workspace is None:
1855+
workspace = "default"
1856+
1857+
if store_name is None:
1858+
store_name = os.path.basename(path)
1859+
f = store_name.split(".")
1860+
if len(f) > 0:
1861+
store_name = f[0]
1862+
1863+
headers = {
1864+
"Content-type": "application/x-sqlite3",
1865+
"Accept": "application/json",
1866+
}
1867+
1868+
url = "{0}/rest/workspaces/{1}/datastores/{2}/file.{3}?filename={2}".format(
1869+
self.service_url, workspace, store_name, file_extension
1870+
)
1871+
1872+
with open(path, "rb") as f:
1873+
r = requests.put(
1874+
url,
1875+
data=f.read(),
1876+
auth=(self.username, self.password),
1877+
headers=headers,
1878+
)
1879+
if r.status_code in [200, 201, 202]:
1880+
return "The geopackage datastore created successfully!"
1881+
else:
1882+
raise GeoserverException(r.status_code, r.content)
1883+
18251884
def publish_featurestore(
18261885
self,
18271886
store_name: str,

tests/data/countries-test.gpkg

96 KB
Binary file not shown.

tests/test_geoserver.py

+11
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,17 @@ def test_styles(self):
126126
)
127127

128128

129+
class TestCreateGeopackageDatastore:
130+
131+
def test_create_geopackage_datastore_from_file(self):
132+
133+
geo.create_gpkg_datastore(f"{HERE}/data/countries-test.gpkg")
134+
store = geo.get_datastore("countries-test")
135+
layer = geo.get_layer("countries-test")
136+
assert store["dataStore"]["name"] == "countries-test"
137+
assert layer["layer"]["name"] == "countries-test"
138+
139+
129140
class TestUploadStyles:
130141

131142
def test_upload_style_from_file(self):

0 commit comments

Comments
 (0)