This project provides a simple API built with FastAPI that takes a list of 2D points and generates a concave hull (alpha shape) based on a given alpha value. The API returns the coordinates of the shape's edges, which is useful for creating boundary shapes around scattered points.
- 2D Concave Hull Calculation: Uses the
alphashape
library to compute the concave hull (alpha shape) of a given set of points. - FastAPI: Provides a fast and interactive API with automatic documentation via Swagger UI.
- Simple Token Verification: Includes a verification token for secure
at least a little bit theaccess to the API. - Python 3.12: Built and deployed using Python 3.12.
- POST /2d_concave_hull/: Receives a JSON payload containing a list of 2D points and a verification token, and returns the alpha shape edges.
POST /2d_concave_hull/?token=your_verification_token&alpha=0
{
"points": [
[0.0, 0.0],
[1.0, 0.0],
[1.0, 1.0],
[0.0, 1.0]
]
}
{
"edges": [
[[0.0, 0.0], [1.0, 0.0]],
[[1.0, 0.0], [1.0, 1.0]],
[[1.0, 1.0], [0.0, 1.0]],
[[0.0, 1.0], [0.0, 0.0]]
]
}
Example of a request using python (inside Dynamo):
import urllib2
import json
def get_concave_hull(points2D, token, alpha = 0.0):
base_url = "http://127.0.0.1:8000/" # Change the base URL when you deploy to production
endpoint = "2d_concave_hull/"
# Encode token to avoid errors
my_token = urllib2.quote(token)
parameters = "?token=" + my_token + "&alpha=" + str(alpha).replace(",", ".")
url = base_url + endpoint + parameters
headers = {'Content-type': 'application/json', "accept": "application/json"}
data = json.dumps(dict(points=points2D))
request = urllib2.Request(url, data, headers)
try:
response = urllib2.urlopen(request)
except urllib2.HTTPError as e:
raise Exception("The request was not successful.\n" + e.read())
return response.read()
- Python 3.12
- Virtualenv
-
Clone the repository:
git clone https://github.com/aecoder-br/python-geometry-api.git cd python-geometry-api
-
Create and activate a virtual environment:
python3.12 -m venv .venv source .venv/bin/activate
Alternatively, on Windows, you can use:
py -3.12 -m venv .venv .venv\Scripts\activate
-
Install the dependencies:
pip install -r requirements.txt
-
Create a
.env
file in the root directory of the project and add your verification token:VERIFICATION_TOKEN=your_verification_token
-
Run the application:
uvicorn main:app --reload
-
Access the API documentation at http://127.0.0.1:8000/docs.
Contributions are welcome! Please open an issue or submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for more details.