Guest User

Untitled

a guest
Jul 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. import json
  2. from shapely import geometry
  3. import rasterio
  4.  
  5. filename = '/path/to/buildings_aoi_2_vegas_img12.geojson'
  6. raster_file = '/path/to/rgb-pansharpen_aoi_2_vegas_img12.tif'
  7.  
  8. # Read geojson file
  9. with open(filename) as src:
  10. fc = json.load(src)
  11.  
  12. # Get affine transform from raster file
  13. with rasterio.open(raster_file) as src:
  14. aff = src.transform
  15.  
  16. ffa = ~aff
  17.  
  18. # Define fuction to build bbox
  19. def darknet_bb_from_feature(feature):
  20. """Build a Darknet style bounding box from a geojson style feature
  21. """
  22. poly = geometry.shape(feature['geometry'])
  23. center = poly.centroid
  24. x_center, y_center = ffa * (center.x, center.y)
  25. bbox = poly.bounds
  26. width = (bbox[2] - bbox[0]) // aff[0]
  27. height = (bbox[3] - bbox[1]) // -aff[4]
  28. out = ('obj_class', x_center, y_center, width, height)
  29. return out
  30.  
  31. # Apply function to all features of feature collection
  32. bb_list = [darknet_bb_from_feature(feat) for feat in fc['features']]
  33. print(bb_list)
Add Comment
Please, Sign In to add comment