AequilibraE / aequilibrae

aequilibrae - Python package for transportation modeling
https://www.aequilibrae.com
Other
166 stars 38 forks source link

Geopandas/Shapely confuse each other, leading to constraint violation and failing to add a link #513

Closed markjd84 closed 7 months ago

markjd84 commented 7 months ago

Really i think this is an upstream bug, but it seems like the sort of thing that may come up again here, so just highlighting to let others decide what to do with it...

We are using Aequilibrae in a consultancy project with a network that will eventually cover a whole continent :)

In adding links per the example, I was using geometry from a shapefile instead of a WKT-CSV, which seemed reasonable at the time (!)

But then my link loading would always get stuck about 10% of the way in, with:

sqlite3.IntegrityError: CHECK constraint failed: TYPEOF(b_node) == 'integer'

Which is weird, because, as far as i can see, Aequilibrae handles the node ids - I hadn't tried to create/change any.

The problem seems to be that loading a link network in from Shapefile (not CSV), some coordinates happen to over-precise and hence GeoPandas produces something like:

>>> record.geometry
<LINESTRING (12 -9, 1.49e-08 -10)>

I guess that it is these coordinates which are then used when generating nodes for project.network - as i understand it, nodes are generated automatically. But something must happen with Shapely (or whatever) that fails with the exponential notation and doesn't create the node. If you can't create the node, you then can't create the link.

If you generate the geometry like so:

new_link.geometry = LineString([Point(12, -9.00002),Point(0.0000000149,-10)])

...you don't get this issue.

I will check whether the same issue comes up if we use WKT-CSV, but it feels like i won't be the last person to try using shapefiles for link networks so worth highlighting (even if we on-hold/close the issue, since it's probably a Shapely problem)

Edit: fwiw, i know that 10 dps in coordinates is silly, and I should just round them...

markjd84 commented 7 months ago

Yes this seems to happen whether you use WKT or Shapefiles...

Like i said, not an Aequilibrae problem, more just me being less familiar with Pandas, but it feels like the sort of thing that might trip others up...

pedrocamargo commented 7 months ago

It would be nice to have an Africa-Wide AequilibraE model, @markjd84 ! ;-P I'll close this, as it is not a bug.

pedrocamargo commented 7 months ago

@markjd84 , since you are using it for paid consulting, please consider having your company help fund the development of AequilibraE.

markjd84 commented 6 months ago

Thanks Pedro - and sorry I missed your reply, my GH notifications were pointing to the wrong place...

On your last point, you are right of course :-) I will raise it internally. I can't make any promises - it's above my pay grade, but I will definitely raise it.

markjd84 commented 6 months ago

And as for the whole-Africa model - watch this space! It may be that our client wants to make it FLOSS once finished...

pedrocamargo commented 6 months ago

I know... we lost that bid to you guys... ;-) I know the group at your client's well.