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

Add an option to return 'date' as 'string' for easy conversion to geojson. #187

Closed
rados opened this issue May 30, 2019 · 2 comments
Closed
Labels

Comments

@rados
Copy link

rados commented May 30, 2019

Hello,

Currently I am converting shape file to a geojson. In my shape file there are dates and I have to pay special attention to convert those dates to strings in order to json-ify them. So this could be a kind of common operation and then maybe the best thing is for the pyshp library to support this inherently somehow. Probably with a kind of flag or something better. I think this could be a simple change in the Reader.__record method, 'D' case.

I am ready to help with a PR.

Thanks,
Rado

@cipri-tom
Copy link

cipri-tom commented Nov 17, 2020

I am having the same problem.

@rados are you doing any manual conversion, or just using the .__geo_interface__ on the file reader ?

How do you find the dates after the conversion ?

Thank you !


An additional problem is if the date is empty, the except branch should probably encode the result as string

pyshp/shapefile.py

Lines 1207 to 1216 in 69c60f6

elif typ == 'D':
# date: 8 bytes - date stored as a string in the format YYYYMMDD.
if value.count(b'0') == len(value): # QGIS NULL is all '0' chars
value = None
else:
try:
y, m, d = int(value[:4]), int(value[4:6]), int(value[6:8])
value = date(y, m, d)
except:
value = value.strip()

@karimbahgat
Copy link
Collaborator

karimbahgat commented Dec 21, 2020

Just pushed new commits to address this, no custom args needed. Date fields are loaded into python date objects for convenience to the user as usual, but calls to geo_interface will automatically convert them back to the original date string, so that they are directly able to be json serialized. Added tests to ensure that all geo_interface calls can be dumped to json.

Based on your suggestion @cipri-tom I also ensured faulty date formats (those that end in the except clause) are converted to unicode/string.

Also added better handling of possible date field null values, although the dbf format doesn't officially support date null values.

Until I push a new release version, you can try it out from the latest Github version.

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

3 participants