33

How can I convert a shapefile (GIS) to text, or, how can I extract the information in a shapefile?

5
  • 1
    This question is on-topic for Super User, but you might get a more detailed, accurate response on gis.stackexchange.com. You don't need to ask your question a second time; if you would like it migrated, flag for a moderator and they can move it for you. Either way, it might be helpful to update your question with more details about what GIS software you are using if you want more specific help. Good luck solving your problem!
    – nhinkle
    Commented Feb 25, 2011 at 7:06
  • 7
    This is an exact duplicate of gis.stackexchange.com/questions/7339/shapefiles-to-text : the answers are already there. They also appear at gis.stackexchange.com/questions/5962/… and gis.stackexchange.com/questions/6806/… .
    – whuber
    Commented Apr 1, 2011 at 19:15
  • @whuber, sorry for asking a duplicate question, however none of the other answers mention pyshp (as far as I can tell), which is what I ended up using, so in a way I am glad I asked. Commented Apr 1, 2011 at 19:51
  • 2 votes left to close duplicate
    – Mapperz
    Commented Apr 1, 2011 at 20:42
  • 1
    @instance I'm glad it worked out for you. A better way to get answers--and a way to get even better answers--is to acknowledge the previous threads and indicate why the existing answers might not suffice in your case. For instance, you might have specified you were looking for a Python solution.
    – whuber
    Commented Apr 2, 2011 at 17:49

12 Answers 12

25

You can use GDAL/OGR with the ogr2ogr command and export to a csv file, for example:

$ ogr2ogr -f CSV output.csv myshape.shp -lco GEOMETRY=AS_WKT

note that you may serialize the geometry in a variety of formats (WKT, XY, XYZ). Please refer to the official documentation, it is very well explained.

4
  • That link is now dead
    – 8bitjunkie
    Commented Nov 16, 2015 at 16:02
  • gdal.org/ogr2ogr.html
    – jsta
    Commented Jan 29, 2016 at 16:00
  • ogr csv driver documentation link updated
    – capooti
    Commented Jan 29, 2016 at 19:23
  • The myshape.shx file must exist in the same directory in order to convert myshape.shp
    – tee
    Commented Feb 1, 2016 at 18:01
13

With QGIS you can open Shapefiles and either export them as CSV or just copy-paste selected features from the map window into a text editor. You'll get WKT of the geometries and all attribute values.

11

If you want to write a little bit of Python, you can use pyshp to read in all the shapefiles and output the X/Y for points or vertices for lines/polygons. Should require a minimal amount of code to get working.

Some sample code from their site:

import shapefile
sf = shapefile.Reader("shapefiles/blockgroups")
shapes = sf.shapes()

Shapes will contain an array of shapes

points = shapes[0].points

Points will contain all the points of the shape at index 0.

3
  • Ill give this a try, thanks. Is there anything in pyshp that would allow me to reproject the shapefiles so that the X/Ys are Latitude/Longitudes? Commented Apr 1, 2011 at 19:29
  • Nevermind my previous comment, the data is already stored in Lat/Lng Commented Apr 1, 2011 at 19:44
  • 2
    Nice! I hadn't heard of pyshp before - could have used it a few months back. Commented Apr 1, 2011 at 20:13
9

If you have ArcGIS 9.x you can use an out of the box script available from ArcToolbox located here, under the ArcGIS install location:

Toolboxes\Samples\Data Management\Features\Write Features To Text File

If you need further formatting of the text file then a Python script would be the way to go...

There is also an ArcGIS Idea called Generate and Ungenerate all vector feature classes from/to ASCII that could use your vote, and this comment has been made:

I just found out at http://forums.arcgis.com/threads/57600-Where-is-the-Generate-tool-in-ArcGIS-10?p=199524#post199524 that there were equivalent tools that already do this in the Sample Tools that have been deprecated (see http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/An_overview_of_the_Samples_toolbox/00pv00000003000000/). Their names are Create Features from Text File and Write Text File from Features.

2
  • Just and FYI, but while this does write out the feature vertices to points to a text file, it does not write the data out as OGC compliant Well Known Text (WKT), in case that is what you are looking for. Commented Apr 22, 2013 at 19:20
  • I fixed a couple of bugs and updated the 9.x developer sample of WriteFeaturesFromTextFile for 10.x, grab it from github.com/maphew/arcplus/blob/master/ArcToolbox/Scripts/… Commented Feb 16, 2015 at 19:40
4

A shape file map consists of the geometry (.shp), the spatial index (.shx), the attribute table (.dbf) and the projection metadata file (.prj). The geometry you could represent in OGC Simple Features SQL style but that won't bring you far. The attribute file you can open with Openoffice or Excel and look at it or export to another format.

4

The utility ogrinfo should be able to do that. I have never tried it with a shapefile but it works fine for me with other GIS formats. Try something like that:

ogrinfo -al myshapefile.shp

On Windows you can easily get a compiled version of ogrinfo by installing FWtools.

2

shapefiles tend to come with several 'companion' files with different extensions. If you just want to extract the attributes, then copying the contents of the .dbf file is sufficient. However, if you want to restore the spatial information, then you will need to add the XY coords into the attribute table first (e.g. AddXY function in ArcMap), then export the attribute table.

2

Using OrbisGIS, you'll be able to open your shapefile and export its content in a CSV file. You'll retrieve a set of lines, each containing a record present in the shapefile. your geometries will be stored as well kown text (WKT) values

1

Just look at the folder with your shapefile in Windows Explorer rather than ArcCatalog and you will see the shapefile is actually several files. Your attribute information, which I am guessing is what you want in a text file is in the .dbf, just open that in Excel, Access, Open Office, etc and save as you wish.

1

load them to PostGIS.

query them something like:

Select st_x(the_geom) as Xcoord, st_y(the_geom) as Ycoord, attr1, attr2 from table1;

you can output the query directly to csv from pgadmin.

if you geometries is linestrings or polygons and you want all the vertexpoints, just do the same but split to points with st_dumppoints.

/Nicklas

1

There seems to be a few questions for this across the site. I just posted an answer at another location. See How to get all the lat/long coords that define a shapefile

1

if you want to draw it on map you can convert shape file into WKT(well known text) format then you can store it in to DB or use it what ever you want to, with your shape file

Not the answer you're looking for? Browse other questions tagged or ask your own question.