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
  • 1
    Without the .prj file, you won't know what coordinate system the data is in. So, while you may be able to get x and y, that is meaningless without a coordinate system.
    – Fezter
    Nov 10 '16 at 5:12
  • 2
    If you use that configuration option you will get an answer. Option is actually first creating the .shx file from .shp. Test with ogrinfo ogrinfo bounds.shp --config SHAPE_RESTORE_SHX TRUE -al -so INFO: Open of bounds.shp' using driver ESRI Shapefile' successful. Layer name: bounds Geometry: Polygon Feature Count: 1 Extent: (500.000000, 280.000000) - (740.000000, 480.000000) Layer SRS WKT: (unknown)
    – user30184
    Nov 10 '16 at 7:14
  • Thanks user30184 for the help. Creating a .shx file using ogrinfo and then using my existing code did the trick. @FezterI was planning to accept shapefile in WGS 1984 only to avoid the ambiguity. Nov 10 '16 at 23:19

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.

Byte 36 Bounding Box Xmin Double Little
Byte 44 Bounding Box Ymin Double Little
Byte 52 Bounding Box Xmax Double Little
Byte 60 Bounding Box Ymax Double Little
Byte 68* Bounding Box Zmin Double Little
Byte 76* Bounding Box Zmax Double Little
Byte 84* Bounding Box Mmin Double Little
Byte 92* Bounding Box Mmax Double Little

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.

  • Unfortunately I do not use C. user30184 comment worked for me. The plan was to accept shapefile in WGS 1984 only, so the prj file was not necessary. Thanks anyway. Nov 10 '16 at 23:24

You can easily get bounding box using fiona.

The minimum bounding rectangle (MBR) or bounds of the collection’s records is obtained via a read-only bounds attribute. https://fiona.readthedocs.io/en/latest/manual.html#format-drivers-crs-bounds-and-schema

import fiona

shp_file = 'a_polygon_shapefile.shp'
c = fiona.open(shp_file)
c.bounds # Returns (minx, miny, maxx, maxy)
(-8.621389, 49.911659, 1.749444, 60.844444)

You can also use package shapefile, which can be found here: https://github.com/GeospatialPython/pyshp

 import shapefile as shp
 sf = shp.Reader("percelen.shp")
 [41988.03170000017, 399987.0051999986, 47011.34809999913, 402276.9079999998]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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