question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make it easier to insert geometries, with documentation and maybe code

See original GitHub issue

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:

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:24 (23 by maintainers)

github_iconTop GitHub Comments

1reaction
simonwcommented, Feb 6, 2022

I wonder if I could implement the above such that this also works:

db["places"].insert(
    {
        "name": "London",
        "point": LongitudeLatitude(-0.118092, 51.509865)
    }
)

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 in conversions=.

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.

1reaction
eyeseastcommented, Feb 6, 2022

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.

from sqlite_utils.conversions import LongitudeLatitude

db["places"].insert(
    {
        "name": "London",
        "lng": -0.118092,
        "lat": 51.509865,
    },
    conversions={"point": LongitudeLatitude("lng", "lat")},
)

I would definitely use that for every CSV I get with lat/lng columns where I actually need GeoJSON.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found