This article will detail the input and output of the several endpoints that is able to operations on shapes
Content Overview:
The base url for the modification of shapes is:
https://demo.ghmaritime.com/api/config/
Additionally, some of the operations requires the user to specify the id of the shape.
However if the id of the shape is unknown, it is possible to construct at GET request via the route /shapes/{name} and then get the id of the shape.
The API will return the entire shape object, which contains the id of the shape.
Define the name of the shape:
name = "<ShapeName>"
Then call the api with a GET request:
GET /shapes/{name}
The API will respond with a JSON object of the shape definitions, which contains the id of the shape:
{ "id": "123" }
import requests
import json
name = "<ShapeName>"
URL = f'https://demo.ghmaritime.com/api/config/shapes/{name}'
headers = {
"Authorization": "Basic <credentials>",
"Content-Type": "application/json",
}
response = requests.get(URL, headers=headers, timeout=10)
print(response.text)
response.raise_for_status()
print(json.dumps(response.json(), indent=2))
The following is a list of parameters that can be altered in the code:
{
"geometry": {
"coordinates": [
[
[
14.05834627976409,
55.1786775171027
],
[
14.71203280320158,
55.57505112961118
],
[
15.56896639695159,
55.56883977888324
],
[
15.93700838913909,
54.97111702573383
],
[
14.50878573288908,
54.59415252295574
],
[
13.77819491257658,
54.79415962743815
],
[
14.05834627976409,
55.1786775171027
]
]
],
"type": "Polygon"
},
"properties": {
"acl": [
"GateHouse"
],
"description": "",
"id": "c0170f78-1913-4af8-8c5d-0857d0bd2162",
"name": "Bornhollm",
"owner": "portadmin",
"user_id": "124bf7c6-0f49-4f90-9117-4fd69e2885bb"
},
"type": "Feature"
}
To create a shape, construct a POST request via the endpoint /shapes, with a GeoJSON object as the request body:
import requests
import json
URL = f'https://demo.ghmaritime.com/api/config/shapes'
headers = {
"Authorization": "Basic <credentials>",
"Content-Type": "application/json",
}
payload = {
"type": "Feature",
"properties": {
"name": "<ShapeName>",
},
"geometry": {
"type": "<ShapeType>",
"coordinates": [
[
[7.691873968739653, 57.83075674025332],
[7.702860296864652, 54.27553453348099],
[12.877420843739653, 54.224184298544515],
[12.910379828114651, 57.84245356620403],
[7.691873968739653, 57.83075674025332]
]
]
}
}
response = requests.post(URL, json=payload, headers=headers, timeout=10)
response.raise_for_status()
print(json.dumps(response.json(), indent=2))
{
"id": "ff4f8e44-be8b-42dc-9ba8-7e5f6f916509"
}
The response is the id of the created shape.
This indicates that the creation of the shape was a success.
When defining the request body, there are certain fields that must be set, depending on the type of the shape.
When creating a circle, its’ field “type” should be “Point”, and the field “radius” should be specified under the “properties” field.
"properties": {
"name": "circle1",
"radius": "100",
},
"geometry": {
"type": "Point",
"coordinates": [
10.91924373456776,
55.73646508111631
]
}
}
No constraints.
When creating a point, only a single pair of latitude and longitude should be specified in the “coordinates” field:
"geometry": {
"type": "Point",
"coordinates": [
10.91924373456776,
55.73646508111631
]
}
}
No constraints.
A shape can be deleted by constructing a DELETE request and specifying the id of the shape in the URL.
DELETE /shapes/{id}
import requests
import json
id = "<ShapeID>"
URL = "https://demo.ghmaritime.com/api/config/{id}"
headers = {
"Authorization": "Basic <credentials>",
"Content-Type": "application/json",
}
response = requests.delete(URL, headers=headers, timeout=10)
response.raise_for_status()
print(json.dumps(response.json(), indent=2))
The following is a list of parameters that can be altered in the code:
{}
To update an already existing shape, construct a PUT request via the endpoint /shapes/{id}.
This allows the user to update an already existing shape with new values, i.e. GeoJSON, name, owner, etc.
import requests
import json
id = "<ShapeID>"
URL = f'https://demo.ghmaritime.com/api/config/shapes/{id}'
headers = {
"Authorization": "Basic <credentials>",
"Content-Type": "application/json",
}
payload = {
"type": "Feature",
"properties": {
"name": "wkt",
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[7.691873968739653, 57.83075674025332],
[7.702860296864652, 54.27553453348099],
[12.877420843739653, 54.224184298544515],
[12.910379828114651, 57.84245356620403],
[7.691873968739653, 57.83075674025332]
]
]
}
}
response = requests.put(URL, json=payload, headers=headers, timeout=10)
response.raise_for_status()
print(json.dumps(response.json(), indent=2))
The following is a list of parameters to be altered in the code:
"payload", can be altered to the desired updates to the shape.{
"id": "f9542c43-3d40-45c1-871b-92a5a3fdeb72"
}
The output-id should correspond to the input-id, since it is the same object that has been modified.
It is also possible to import a shape from different formats. This is done by constructing a PUT request to the /shapes/import endpoint with the request body containing one of the following formats:
Additionally, the name and type should be specified for the shape in the parameters of the request.
Importing a GeoJSON file is done by reading the file and converting it into a JSON object that can be sent as the request body.
import requests
import json
URL = f'https://demo.ghmaritime.com/api/config/shapes/import'
headers = {
"Authorization": "Basic <credentials>",
"Content-Type": "application/json",
}
params = {
"name": "<ShapeName>",
"type": "<ImportFileType>"
}
with open("<path>.geojson", "r") as f:
data = json.load(f)
response = requests.post(URL, json=data, params=params, headers=headers, timeout=10)
response.raise_for_status()
print(json.dumps(response.json(), indent=2))
The following is a list of parameters to alter in the code:
"geojson".{
"id": "c90aa57f-9123-4b48-81a2-0fbce59116fd"
}
To import a Well Known Text (WKT) file, it is important to note that the "Content-Type" field should be set to "text/plain".
import requests
import json
URL = f'https://demo.ghmaritime.com/api/config/shapes/import'
headers = {
"Authorization": "Basic <credentials>",
"Content-Type": "text/plain",
}
params = {
"name": "<ShapeName>",
"type": "<ImportFileType>"
}
with open("<path>.wkt", "r") as f:
data = f.read().strip()
response = requests.post(URL, data=data, params=params, headers=headers, timeout=10)
print(response.text)
response.raise_for_status()
print(json.dumps(response.json(), indent=2))
The following is a list of parameters to alter in the code:
"wkt".{
"id": "22ce9dda-0f38-4d6a-8087-3c7d57d06d38"
}
When importing a ZIP file, it should be encoded as base64 to import a shape in ZIP format.
It is important to note that the "Content-Type" field is different for zip files, where they should be specified as "application/octet-stream".
The following code demonstrates how to do this:
import requests
import json
import base64
URL = f'https://demo.ghmaritime.com/api/config/shapes/import'
headers = {
"Authorization": "Basic <credentials>",
"Content-Type": "application/octet-stream",
}
params = {
"name": "<ShapeName>",
"type": "<ImportFileType>"
}
with open("<path>.zip", "rb") as f:
binary_data = f.read()
base64_data = base64.b64encode(binary_data).decode("utf-8")
response = requests.post(URL, data=base64_data, params=params, headers=headers, timeout=10)
response.raise_for_status()
print(json.dumps(response.json(), indent=2))
The following is a list of parameters to alter in the code:
"zip".{
"id": "2111d2df-5ee9-4ec8-acc8-f1fd1f138699"
}
To export a shape, construct a GET request via the /shapes/export/{id} endpoint with parameters that specify the format of the file.
The 3 possible types of exports is as follows:
The construction of the GET request work similar for each type of export; however, they differ in the “format” field in the parameters.
The response from the API is an object in the chosen format.
import requests
import json
id = "<ShapeID>"
URL = f'https://demo.ghmaritime.com/api/config/shapes/export/{id}'
headers = {
"Authorization": "Basic <credentials>",
"Content-Type": "application/json",
}
params = {
"format": "<ExportFormat>"
}
response = requests.get(URL, params=params, headers=headers, timeout=10)
response.raise_for_status()
print(json.dumps(response.json(), indent=2))
The following is a list of parameters to alter in the code: