Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- from shapely import geometry
- import rasterio
- filename = '/path/to/buildings_aoi_2_vegas_img12.geojson'
- raster_file = '/path/to/rgb-pansharpen_aoi_2_vegas_img12.tif'
- # Read geojson file
- with open(filename) as src:
- fc = json.load(src)
- # Get affine transform from raster file
- with rasterio.open(raster_file) as src:
- aff = src.transform
- ffa = ~aff
- # Define fuction to build bbox
- def darknet_bb_from_feature(feature):
- """Build a Darknet style bounding box from a geojson style feature
- """
- poly = geometry.shape(feature['geometry'])
- center = poly.centroid
- x_center, y_center = ffa * (center.x, center.y)
- bbox = poly.bounds
- width = (bbox[2] - bbox[0]) // aff[0]
- height = (bbox[3] - bbox[1]) // -aff[4]
- out = ('obj_class', x_center, y_center, width, height)
- return out
- # Apply function to all features of feature collection
- bb_list = [darknet_bb_from_feature(feat) for feat in fc['features']]
- print(bb_list)
Add Comment
Please, Sign In to add comment