Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance combination with shapely #249

Open
1 task
forestbat opened this issue Oct 24, 2022 · 2 comments
Open
1 task

Enhance combination with shapely #249

forestbat opened this issue Oct 24, 2022 · 2 comments

Comments

@forestbat
Copy link

forestbat commented Oct 24, 2022

When I try to calculate something such as distance between geometries, I will use shapely however shapely's geometry is not hashable,so I must use shapely's wkt such as:
dict[key] = shapely_geometry.wkt
WKT is string, however, shapely's wkt probably occurs integer such as:
'LINESTRING (121 43.422917……121.047917 43.460417)'
And if I try to convert geometries of pyshp to wkt with my code:

def shape_type_to_wkt(shape: Shape):
    geo_shpfile = shape.__geo_interface__
    geo_class = geo_shpfile['type'].upper()
    wkt: str = geo_class + ' ('
    geo_coords = []
    if geo_class == 'POINT':
        geo_coords.append(geo_shpfile['coordinates'])
    else:
        geo_coords = list(geo_shpfile['coordinates'])
    for coord_tuple in geo_coords:
        wkt += str(coord_tuple[0])
        wkt += ' '
        wkt += str(coord_tuple[1])
        wkt += ', '
    wkt = wkt.rstrip(', ')
    wkt += ')'
    return wkt

WKT generated from this method will occurs float64
'LINESTRING (121.0 43.422917……121.047917 43.460417)'
This brings me some trouble,I must use shape_type_to_wkt(shapely.loads(nearest_line.wkt)) to convert probable integer to float in shapely's wkt.
Above code is my business code and I only tested it in 2-dimensions qgis layer which only has Point and LineString, so I hope you can enchance combination for pyshp and shapely, don't let this irritating problem to occur.

Contributions

  • I am interested in implementing the described feature request and submit as a PR.
@karimbahgat
Copy link
Collaborator

I'm not sure I fully understand your problem. You're saying shapely's wkt method encodes integer coordinates without decimals, while your own pyshp wkt function will represent those integers with decimals. Why is this a problem? If you really need them to produce the same exact wkt, adding a Python check for .isinteger() in your function should fix this. And what are suggesting pyshp is doing wrong or could be doing to fix this problem?

I have considered the benefit of having a wkt property for pyshp if that's what you're suggesting, so would happily accept PRs for this.

As for interoperability with shapely, this can easily be done with geojson dicts or strings (which should be hashable):

# from shapely to geojson to pyshp
geoj = shapely_obj.__geo_interface__
writer.shape(geoj) # this is the pyshp writer which accepts writing shapes from geojson dicts

# from pyshp to geojson to shapely
geoj = pyshp_shape.__geo_interface__
pyshp_obj = shapely.geometry.shape(geoj)

But maybe I'm missing something.

@forestbat
Copy link
Author

# from shapely to geojson to pyshp
geoj = shapely_obj.__geo_interface__
writer.shape(geoj) # this is the pyshp writer which accepts writing shapes from geojson dicts

# from pyshp to geojson to shapely
geoj = pyshp_shape.__geo_interface__
pyshp_obj = shapely.geometry.shape(geoj)

Thanks for your code. And yes,I hope pyshp can have a wkt property.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants