Skip to content

Unexpected error: Unable to find a ring sample point for multiple files #207

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

Closed
tmesis opened this issue Dec 15, 2020 · 3 comments
Closed
Labels

Comments

@tmesis
Copy link

tmesis commented Dec 15, 2020

Have an issue similar to #205. When using the __geo_interface__ to convert Shapefiles to GeoJSON, the following exception is encountered: Unexpected error: Unable to find a ring sample point. This happens for a subset of files downloaded from US Census Bureau's TIGER/LINE database. An example is tl_2019_37_cousub.zip (ftp://ftp2.census.gov/geo/tiger/TIGER2019/COUSUB/tl_2019_37_cousub.zip). Not sure if this and #205 arise from same issue.

For reference, my code is thus:

import json
from pathlib import Path
from typing import Dict, Iterator, TextIO, Union

import shapefile


def parse_features(shp: Union[Path, str], shx: Union[Path, str], dbf: Union[Path, str]) -> Iterator[Dict]:
    reader = shapefile.Reader(*map(str, (shp, shx, dbf)))
    fields = reader.fields[1:]
    field_names = [field[0] for field in fields]
    for shape, record in zip(reader.iterShapes(), reader.iterRecords()):
        yield dict(type='Feature', geometry=shape.__geo_interface__, properties=dict(zip(field_names, record)))


def shp_to_json(f: TextIO, shp: Union[Path, str], shx: Union[Path, str], dbf: Union[Path, str]):
    for features in parse_features(shp, shx, dbf):
        f.write(
            json.dumps({'type': 'FeatureCollection', 'features': features}, indent=2) + '\n'
        )
@karimbahgat
Copy link
Collaborator

This indeed seems to be the same issue as #205. While I fixed the error for that issue, I forgot to push it as a new bugfix version to pyshp. I just tried locally with the file you linked to and it worked without issue. Will push the new 2.1.3 to pyshp shortly, in the meanwhile, just install pyshp from GitHub: pip install --upgrade --no-deps --force-reinstall https://github.com/GeospatialPython/pyshp/archive/master.zip.

@karimbahgat
Copy link
Collaborator

As an aside, I see in your example code that you are looping through each individual feature of a shapefile, and dumping a FeatureCollection for each individual feature. If this is what you intend, you need to be wrapping your individual feature in list brackets for it to be correct geojson. Something like this:

def shp_to_json(f: TextIO, shp: Union[Path, str], shx: Union[Path, str], dbf: Union[Path, str]):
    for feature in parse_features(shp, shx, dbf):
        f.write(
            json.dumps({'type': 'FeatureCollection', 'features': [feature]}, indent=2) + '\n'
        )

I'd also recommend just iterating the shapefile and letting the feature generate the feature geojson for you:

    for feature in reader:
        yield feature.__geo_interface__

@tmesis
Copy link
Author

tmesis commented Dec 21, 2020

Thank you for the response. I caught the [feature] issue after recent load into the database. Splitting out the features because of a limit on JSON doc length. Will test the new code. Appreciate your prompt engagement.

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

No branches or pull requests

2 participants