Make it easier to insert geometries, with documentation and maybe code
See original GitHub issueIn 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:
Issue Analytics
- State:
- Created 2 years ago
- Comments:24 (23 by maintainers)
Top Results From Across the Web
geomfromtext insert in spatialite from retrived text
Attempting to build a data entry form, I would like to create geometry points from the longitude.get() and latitude.get() values. However, the ...
Read more >Solved: Insert geometry from ESRI feature class to MSSQL s...
I am trying to do what seems to be a simple operation: 1. Iterate through a feature class 2. Insert found rows to...
Read more >Chapter 4. Data Management - PostGIS
PostGIS implements the OGC Simple Features model by defining a PostgreSQL data type called geometry . It represents all of the geometry subtypes...
Read more >Documenting Python Code: A Complete Guide
When you write code, you write it for two primary audiences: your users and your developers (including yourself). Both audiences are equally important....
Read more >How to Add Numbered Circles/Labels On Top of a Picture in ...
This is perfectly easy to do in MS PowerPoint. You simply use Insert -->Shapes, select the circle shape, type a number inside the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I wonder if I could implement the above such that this also works:
This feels like a very natural way to work with single inserts.
The challenge is writing the code inside
.insert_all()
such that it can handle these special objects in the input column values in addition to them being passed inconversions=
.I’m feeling very good about this direction in general though, it feels like it takes the existing but not particularly elegant
conversions=
mechanism and upgrades it to be far more useful, while maintaining backwards compatibility.I like the idea of having stock conversions you could import. I’d actually move them to a dedicated module (call it
sqlite_utils.conversions
or something), because it’s different from other utilities. Maybe they even take configuration, or they’re composable.I would definitely use that for every CSV I get with lat/lng columns where I actually need GeoJSON.