Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. import random
  2. from osgeo import ogr,gdal
  3.  
  4. InputImage = r'C:UsersUsuarioDesktopQGIS-PoolOrtofotosPNOA_MA_OF_ETRS89_HU31_h50_0823.tif'
  5. Shapefile = r'C:UsersUsuarioDesktopQGIS-PoolGitPool_shapefilePool.shp'
  6. RasterFormat = 'GTiff'
  7. PixelRes = 0.5
  8. VectorFormat = 'ESRI Shapefile'
  9. folder = 'C:\Users\Usuario\Desktop\QGIS-Pool\Git\images\'
  10. tilemapping = {}
  11.  
  12. # Open datasets
  13. Raster = gdal.Open(InputImage, gdal.GA_ReadOnly)
  14. Projection = Raster.GetProjectionRef()
  15.  
  16. VectorDriver = ogr.GetDriverByName(VectorFormat)
  17. VectorDataset = VectorDriver.Open(Shapefile, 0) # 0=Read-only, 1=Read-Write
  18. layer = VectorDataset.GetLayer()
  19. FeatureCount = layer.GetFeatureCount()
  20. print("Feature Count:",FeatureCount)
  21.  
  22. # Iterate through the shapefile features
  23. Count = 0
  24. for feature in layer:
  25. Count += 1
  26. #print("Processing feature "+str(Count)+" of "+str(FeatureCount)+"...")
  27.  
  28. geom = feature.GetGeometryRef()
  29. xmid, xmid, ymid, ymid = geom.GetEnvelope()
  30. xmid = xmid + random.randint(-40, 40)
  31. ymid = ymid + random.randint(-40, 40)
  32. xmin, ymin = xmid - 50, ymid - 50
  33. xmax, ymax = xmid + 50, ymid + 50
  34.  
  35. # Create raster
  36. OutTileName = folder + str(Count) + '.tif'
  37. OutTile = gdal.Translate(OutTileName, Raster, width=224, height=224, projWin = [xmin, ymax, xmax, ymin])
  38. OutTile = None # Close dataset
  39.  
  40. centers = []
  41. for feature1 in layer:
  42. geom1 = feature1.GetGeometryRef()
  43. x, x, y, y = geom1.GetEnvelope()
  44. if (xmin < x) & (x < xmax) & (y > ymin) & (y < ymax):
  45. x = (x - xmin) / (xmax - xmin)
  46. y = (y - ymin) / (ymax - ymin)
  47. centers.append((x,y))
  48.  
  49.  
  50. if len(centers) > 0:
  51. tilemapping[Count] = centers
  52.  
  53.  
  54. # Close datasets
  55. Raster = None
  56. VectorDataset.Destroy()
  57. print("Done.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement