-1

There is already few existing questions about this topic, but I unfortunately did not find something that could fix my problem...

I have a point Lat, Long coordinate i.e. Lat= 10 and Long = 10. I want to create a shapefile of a 0.5 degree bounding box around this point, so the bounding box should be as follow:

  1. minimum Long= 9.75
  2. minimum Lat = 9.75
  3. maximum Long = 10.25
  4. maximum Lat = 10.25

Does anyone knows how to do that in Python without GIS software?

I have no ESRI or QGIS. I thought that it would be possible to create a shapefile using bounding box coordinates with Python. The only info I have are the coordinates of the bounding box. I try using pyshp package pypi.org/project/pyshp but it is not obvious how should I proceed.

closed as off-topic by LaughU, ahmadhanb, whyzar, neogeomat, PolyGeo Jan 31 at 21:51

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "When seeking help to debug/write/improve code always provide the desired behavior, a specific problem/error and the shortest code (as formatted text, not pictures) needed to reproduce it in the question body. Providing a clear problem statement and a code attempt helps others to help you." – LaughU, ahmadhanb, whyzar, PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    What have you already tried? in which GIS Software you want to use the tool? – LaughU Jan 31 at 14:56
  • There are already a few questions with good answers. Here's one for ESRI, and one for QGIS. Both provide solutions with python and could be adapted to newer versions. – Gabriel C. Jan 31 at 15:06
  • I think my question is unclear. I have no ESRI or QGIS. I thought that it would be possible to create a shape file using bounding box coordinates with python. The only info I have are the coordinates of the bounding box. – steve Jan 31 at 15:07
  • I try tio find a way using pyshp package pypi.org/project/pyshp but it is not obvious how should i proceed – steve Jan 31 at 15:08
3

Well, at least this worked for me, following the Docs (which seem a bit outdated to me) and using the internal module documentation:

import shapefile as shp
from osgeo import osr

centroid_x = 10.
centroid_y = 10.
bbox_radius = 0.25
target_epsg = 4326
dst_file = r'd:\working\testing\test.shp'

w = shp.Writer()
# assign POLYGON (5) as shapeType
w.shapeType = 5
# add a "name" field of type "Character"
w.field('name', 'C')
# coordinates in clockwise rotation
w.poly([[[centroid_x - bbox_radius, centroid_y + bbox_radius],  # upper left
        [centroid_x + bbox_radius, centroid_y + bbox_radius],   # upper right
        [centroid_x + bbox_radius, centroid_y - bbox_radius],   # lower right
        [centroid_x - bbox_radius, centroid_y - bbox_radius],   # lower left
        [centroid_x - bbox_radius, centroid_y + bbox_radius]]]) # first point again
w.record('testpolygon')
w.save(dst_file)
# create ESRI prj-file
sr = osr.SpatialReference()
sr.ImportFromEPSG(target_epsg)
sr.MorphToESRI()
with open(dst_file.replace('.shp', '.prj'), 'w') as prj:
    prj.write(sr.ExportToWkt())

Unfortunately, 'pyshp' seems to lack the possibility to set a coordinate reference system, so you have to do that manually or use other modules, like osr from osgeo. I added the lines to create the projection to the code.

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