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.

ENH: Prevent creation of geometries with NaN coordinates

See original GitHub issue

It seems that pygeos incorrectly reports that ‘has_z’ is false in case the first z-coordinate is NaN. This leads to the z-dimension getting culled in shapely/geopandas.

Traced this bug after getting funky results in geopandas. https://github.com/geopandas/geopandas/issues/1888

import numpy as np
import pygeos
import shapely.wkb

coords1 = [
    (1, 2, 3),
    (1, 1, np.NaN),
]

coords2 = [
    (1, 1, np.NaN),
    (1, 2, 3),
]

for coords in [coords1, coords2]:
    geom1 = pygeos.creation.linestrings(coords)
    print(pygeos.geometry.get_coordinate_dimension(geom1))
    print(pygeos.predicates.has_z(geom1))
    print(pygeos.coordinates.get_coordinates(geom1, include_z=True))
    geom2 = shapely.wkb.loads(pygeos.to_wkb(geom1))
    print(np.array(geom2.coords))
    print('\n')

Output

3
True
[[ 1.  2.  3.]
 [ 1.  1. nan]]
[[ 1.  2.  3.]
 [ 1.  1. nan]]


3
False
[[ 1.  1. nan]
 [ 1.  2.  3.]]
[[1. 1.]
 [1. 2.]]

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
caspervdwcommented, May 19, 2021

I’d like to make a bold proposal: block NaN in geometry creation functions (just skip NaN coordinates, like we do with None for polygon/multi geometries)

Associated with this, we might create force_2d and force_3d functions. The force_3d takes an optional argument z_value, which cannot be NaN and defaults to 0.0 (like with PostGIS: https://postgis.net/docs/ST_Force_3D.html)

0reactions
brendan-wardcommented, Mar 25, 2021

Using a well-defined NODATA value for Z values seems better than using NaN; in the raster they are actually a number that has a NODATA interpretation, as opposed to something that is unrepresentable as a number. The raster dataset should define this NODATA value, so you can just use that, right? In either case, you run the risk of any interpolation applied to Z values unless you handle the NODATA values properly, and using a defined number sidesteps all the issues around GEOS and NaN.

Read more comments on GitHub >

github_iconTop Results From Across the Web

d3 fitSize gives NaN values - Stack Overflow
I want to make my d3 map fit to a svg container created with dynamic width and height attributes. It works so far...
Read more >
#2179 (ST_MakeValid support for NaN values.) – PostGIS
Pretend all NaN values are an arbitrary value (0.0) ? Drop all Coordinates with any NaN ordinate value (including Z and M?) Return...
Read more >
What the different data filters do? - libpointmatcher
This filter can be applied to remove points which contain a NaN coordinate, thus producing a "clean" dataset. Required descriptors: none. Output descriptor: ......
Read more >
Using MATLAB Graphics
To create a text annotation using the text function, you must specify the text and its location in the graph, using x- and...
Read more >
gmt — GMT 6.5.0 documentation
Allows users to create a matrix of panels with automatic labeling and ... In case of longitude–latitude plots, this will keep the spacing...
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