Is .shp file, without the dbf, .shx, .prj
or others, enough to get the bounding box?
I am trying to get user to upload their shapefiles in a web page. I want to obtain longitude and latitude from the point shapefile, and bounding box (long,lat long,lat) from the polygon shapefile (both the shapefile will contain single shape/feature). I would like to think .shp
file is enough to obtain the shape, but when I use fiona
, it gives me an error. Is there any package that might help?
The code looks like this:
import fiona
from shapely.geometry import shape
shp_file = 'a_polygon_shapefile.shp'
c = fiona.open(shp_file)
# first record
first_shape = c.next()
box_topY = shape(first_shape['geometry']).bounds[3]
box_bottomY = shape(first_shape['geometry']).bounds[1]
box_rightX = shape(first_shape['geometry']).bounds[2]
box_leftX = shape(first_shape['geometry']).bounds[0]
The error I get is:
Unable to open a_polygon_shapefile.shx or a_polygon_shapefile.SHX.Try --config SHAPE_RESTORE_SHX true to restore or create it
Best Answer
Technically speaking, you can get the bounding box just from the main
.shp
file, according to the shapefile white paper, page 4. The values of the bounding box is located at the following bytes in the file header for the.shp
file.On the other hand, unless you have external knowledge about the coordinate system, which should be contained in the
.prj
file, you wouldn't be able to use the values of the bounding box, as pointed out in the comments.If you know how to read a binary file, say via C programming or another language, you can easily extract the bounding box itself using the offsets above.