3

I'm using pyshp to create new shapefiles with Python 3. My code runs without errors, but when I open the resulting file in ArcMap I get this error:

The following datasource you added is missing spatial reference information.

This is not surprising, since I never told my code that the coordinates in the shapefile are supposed to be in EPSG:3006. How do I set this property in pyshp?

Here's some short example code. I want to tell the writer shape that the coordinates in start and stop are in EPSG:3006.

import shapefile
start = [6400137, 321751] # Coordinates in EPSG:3006
stop = [6400095, 319357] # Coordinates in EPSG:3006
with shapefile.Writer("my_shapefile") as shape:
 shape.fileType = 3 # Polyline
 shape.field("FOO", "C", size = 32)
 shape.field("BAR", "C", size = 32)
 shape.line([[start, stop]])
 shape.record("foo", "bar")
asked Jun 3, 2019 at 9:49
2
  • 2
    Unlike osgeo.ogr, Fiona or GeoPandas, Pyshp doesn't manage projection files (look at Create a .prj Projection File for a Shapefile) Commented Jun 3, 2019 at 15:04
  • Thanks @gene! Your comment, and especially the last link, solved my problem. If you would post it as an answer I would be happy to upvote and accept. Commented Jun 3, 2019 at 21:47

1 Answer 1

2

Information about the coordinate system is stored in a separate projection file, that should have the same name as the shapefile, but a .prj extention.

As gene points out in comments, the Pyshp library doesn't handle projection files. So you will have to write the file yourself. Here's an example of how to do that:

# create the PRJ file
prj = open("%s.prj" % filename, "w")
epsg = 'GEOGCS["WGS 84",'
epsg += 'DATUM["WGS_1984",'
epsg += 'SPHEROID["WGS 84",6378137,298.257223563]]'
epsg += ',PRIMEM["Greenwich",0],'
epsg += 'UNIT["degree",0.0174532925199433]]'
prj.write(epsg)
prj.close()

If you want to find the correct file content for your coordinate system, I recommend you simply create a shapefile with that coordinate system in e.g. ArcCatalog, and then just copy paste the content of the created projection file.

answered Apr 20, 2020 at 15:31

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.