Guest User

Untitled

a guest
Nov 18th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. # code
  2. from descartes import PolygonPatch
  3. import geopandas as gp
  4. import pysal as ps
  5.  
  6. from matplotlib import pyplot as plt
  7. from matplotlib.patches import Polygon as mpl_Polygon
  8. from matplotlib.collections import PatchCollection
  9.  
  10. shapefile = "filepolygon.shp"
  11.  
  12. df_map_elements = gp.GeoDataFrame.from_file(shapefile)
  13.  
  14.  
  15. df_map_elements["mpl_polygon"] = np.nan
  16. print type(df_map_elements['mpl_polygon'])
  17.  
  18. df_map_elements['mpl_polygon'] = df_map_elements['mpl_polygon'].astype(object)
  19. for self_index, self_row_df in df_map_elements.iterrows():
  20. m_polygon = self_row_df['geometry']
  21. poly=[]
  22. if m_polygon.geom_type == 'MultiPolygon':
  23. for pol in m_polygon:
  24. poly.append(PolygonPatch(pol))
  25. else:
  26. poly.append(PolygonPatch(m_polygon))
  27. df_map_elements.set_value(self_index, 'mpl_polygon', poly)
  28.  
  29. sourceDataGdalObj = gdal.Open("rgb_landsat.tif")
  30. ulx, xres, xskew, uly, yskew, yres = sourceDataGdalObj.GetGeoTransform()
  31. lrx = ulx + (sourceDataGdalObj.RasterXSize * xres)
  32. lry = uly + (sourceDataGdalObj.RasterYSize * yres)
  33. RasterBBox = [ulx, lrx, uly, lry]
  34.  
  35. outputArray = []
  36. for band in range(sourceDataGdalObj.RasterCount):
  37.  
  38. dataGdalArray = sourceDataGdalObj.GetRasterBand(band+1).ReadAsArray()
  39. outputArray.append(dataGdalArray)
  40.  
  41. dict_mapindex_mpl_polygon = df_map_elements['mpl_polygon'].to_dict()
  42. fig, ax = plt.subplots()
  43.  
  44. for c_l ,patches in dict_mapindex_mpl_polygon.items():
  45. p = PatchCollection(patches,color='none',lw=2.5,edgecolor='black')
  46. ax.imshow(np.dstack(outputArray), interpolation='nearest', extent=RasterBBox)
  47. ax.add_collection(p)
  48.  
  49. ax.autoscale_view()
Add Comment
Please, Sign In to add comment