cant open shape file with GeoPandas

2024/10/4 18:55:36

I don't seem to be able to open the zip3.zip shape file I download from (http://www.vdstech.com/usa-data.aspx)

Here is my code:

import geopandas as gpd
data = gpd.read_file("data/zip3.shp")

this gives me the error:

CPLE_AppDefinedError: b'Recode from CP437 to UTF-8 failed with the error: "Invalid argument".'
Answer

As per my answer on this question, seems like your dataset contains non-UTF characters. If you are facing this similar issue, chances are that using encoding-"utf-8" won't help as Fiona's open() call will still fail.

If other solutions don't work, two solutions I propose that solved this issue are:

  1. Open your shapefile on a GIS editor (like QGis), then save it again making sure you select the Encoding option to "UTF-8". After this you should have no problem when calling gpd.read_file("data/zip3.shp").

  2. You can also achieve this format change in Python using GDAL, by reading your shapefile and saving it again. This will effectively change the encoding to UTF-8, as this is the default encoding as indicated in the docs for the CreateDataSource() method. For this try the following code snippet:

    from osgeo import ogrdriver = ogr.GetDriverByName("ESRI Shapefile")
    ds = driver.Open("nbac_2016_r2_20170707_1114.shp", 0) #open your shapefile
    #get its layer
    layer = ds.GetLayer()#create new shapefile to convert
    ds2 = driver.CreateDataSource('convertedShape.shp')
    #create a Polygon layer, as the one your Shapefile has
    layer2 = ds2.CreateLayer('', None, ogr.wkbPolygon)
    #iterate over all features of your original shapefile
    for feature in layer:#and create a new feature on your converted shapefile with those featureslayer2.CreateFeature(feature)
    #proper closing
    ds = layer = ds2 = layer2 = None 
    
https://en.xdnf.cn/q/70569.html

Related Q&A

How to fix the error :range object is not callable in python3.6 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

pyspark: Save schemaRDD as json file

I am looking for a way to export data from Apache Spark to various other tools in JSON format. I presume there must be a really straightforward way to do it.Example: I have the following JSON file jfil…

How to start a background thread when django server is up?

TL;DR In my django project, where do I put my "start-a-thread" code to make a thread run as soon as the django server is up?First of all, Happy New Year Everyone! This is a question from a n…

Can I use SQLAlchemy relationships in ORM event callbacks? Always get None

I have a User model that resembles the following:class User(db.Model):id = db.Column(db.BigInteger, primary_key=True)account_id = db.Column(db.BigInteger, db.ForeignKey(account.id))account = db.relatio…

Elegant way to transform a list of dict into a dict of dicts

I have a list of dictionaries like in this example:listofdict = [{name: Foo, two: Baz, one: Bar}, {name: FooFoo, two: BazBaz, one: BarBar}]I know that name exists in each dictionary (as well as the oth…

sharpen image to detect edges/lines in a stamped X object on paper

Im using python & opencv. My goal is to detect "X" shaped pieces in an image taken with a raspberry pi camera. The project is that we have pre-printed tic-tac-toe boards, and must image t…

How to change the color of lines within a subplot?

My goal is to create a time series plot for each column in my data with their corresponding rolling mean. Id like the color of the lines across subplots to be different. For example, for gym and rollin…

Cythons calculations are incorrect

I implemented the Madhava–Leibniz series to calculate pi in Python, and then in Cython to improve the speed. The Python version:from __future__ import division pi = 0 l = 1 x = True while True:if x:pi…

Python: NLTK and TextBlob in french

Im using NLTK and TextBlob to find nouns and noun phrases in a text:from textblob import TextBlob import nltkblob = TextBlob(text) print(blob.noun_phrases) tokenized = nltk.word_tokenize(text) nouns =…

How can I run a script as part of a Travis CI build?

As part of a Python package I have a script myscript.py at the root of my project and setup(scripts=[myscript.py], ...) in my setup.py.Is there an entry I can provide to my .travis.yml that will run my…