Make it easier to insert geometries, with documentation and maybe code
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 2.2k
- Forks
- 172
- Avg merge
- 9m
- Merged PRs (30d)
- 1
Description
In playing with the new SpatiaLite helpers from #385 I noticed that actually populating geometry columns is still a little bit tricky. Here's what I ended up doing:
import httpx, sqlite_utils
db = sqlite_utils.Database("/tmp/spatial.db")
attractions = httpx.get("https://latest.datasette.io/fixtures/roadside_attractions.json?_shape=array").json()
db["attractions"].insert_all(attractions, pk="pk")
# Schema of that table is now:
# CREATE TABLE [attractions] (
# [pk] INTEGER PRIMARY KEY,
# [name] TEXT,
# [address] TEXT,
# [latitude] FLOAT,
# [longitude] FLOAT
# )
db.init_spatialite()
db["attractions"].add_geometry_column("point", "POINT")
db.execute("""
update attractions set point = GeomFromText(
'POINT(' || longitude || ' ' || latitude || ')', 4326
)
""")
That last line took some figuring out - especially the need for the SRID of 4326, without which I got this error:
IntegrityError: attractions.point violates Geometry constraint [geom-type or SRID not allowed]
It would be good to both document this in more detail, but ideally also to come up with a more obvious pattern for inserting common types of spatial data.
Also related:
- #398
- #79
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the insertion example in this issue and review the existing init_spatialite() and add_geometry_column() APIs, along with related issues #398 and #79. Determine whether the result should be expanded documentation, a simpler insertion pattern, or both; done means the common spatial-data workflow and SRID requirement are clear and supported.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, sqlite
- Domain
- databases
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100