Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am trying to import a shapefile into PostGIS using the ogr2ogr tools that are built into the FWTools package. In my import process I am receiving an error that I don't know how to deal with.

ERROR 4: Unable to open city.shx or city.SHX. 
FAILURE: Unable to open datasource `city.shp' with the following drivers.

My code is:

ogr2ogr -overwrite -f "PostgreSQL" PG:"host=myhost user=myuser dbname=mydb password=mypass" city.shp

I have confirmed that the shapefile does not have the associated SHX file, but now what? To make it worse, I have a whole lot of files to import that I assume will have the same issue because they also do not have an SHX. Any thoughts on how I can resolve this problem?

share|improve this question
1  
Thank you all for your responses. As it turned out, when I unzipped the files, the SHX must not have been extracted properly. I went back and re-extracted the data and everything is working as expected. –  RyanDalton Mar 28 '11 at 20:28

4 Answers 4

up vote 3 down vote accepted

I just tested. Deleted the shx of a copy of a shape file and used this utility.
It worked a million! Try it.

share|improve this answer
1  
Excellent utility! It worked just as reported and has a little button to allow you to rebuild the SHX. I ran it on the file missing the shx and the after rebuilding the shx it works! Thanks. –  RyanDalton Mar 28 '11 at 20:42
    
as noted in other post @benjamingolder: if the geometry has attributes you might check to see that the right values get indexed to the right geometry. Probably not going to be a problem as long as the dbf files haven't been sorted or opened in any other software and saved. –  Brad Nesom Mar 28 '11 at 21:38

A 'shapefile' really doesn't exist without the 3 minimum component files (.shp, .shx, .dbf) If the data that you are working with is a point shapefile and the coordinates also exist in attribute columns in the .dbf, you should be able to create a new shapefile.

share|improve this answer

The .shx file is mandatory. A shapefile without .shx is incomplete and cannot be used.

Maybe the thread "How to regenerate a shx file having shp and dbf?" can help though.

share|improve this answer

If you are comfortable with python, you can use pyshapefile to create new shapefiles that can be read by ogr2ogr. I don't believe that it reads the .shx file, and I'm not sure what necessary information is contained in the .shx file for simply reading gis data.

The .shx file is used to hold an index for quickly searching back and forth between features. The .dbf file holds the attribute data for each feature, but the geometry information is stored in the .shp file in a binary format (including z and m values, if they exist). Therefore, without the .shx file you still have all the geometry and attribute data (assuming you have .dbf files). If you only have .shp files, and no .dbf or .shx files, then you have all the geometry, but you have no attributes or indexing.

The value of the .shx file, if I understand it correctly, is that it ensures a secure mapping between the .dbf data and the features in the .shp file. It is also used by programs for searching back and forth in the file. However, in every shapefile I have encountered, the geometry features stored in the .shp file and the attribute information stored in the .dbf file are in the same order. In other words, the first feature in the .shp would correspond to the first row of attributes in the .dbf. So as long as your files are in a similar order, you should be fine.

share|improve this answer
    
also, you might want to try the postgres shp loader: postgis.refractions.net/docs/ch04.html#shp2pgsql_usage –  BenjaminGolder Mar 28 '11 at 20:13
    
.shp and .dbf file can be in a different order (if you deleted/edited features). Only the .shx file contains the information about how to match them. –  underdark Mar 28 '11 at 20:14
    
I was simultaneously editing it to reflect that, but have you encountered the shp and dbf to be in a different order? –  BenjaminGolder Mar 28 '11 at 20:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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