admin管理员组文章数量:1355594
I have successfully created a filled contour map with UTM data from a numpy array and exported to a shape file, but the shape file only has polygons with no other data. The partial code is:
#Convert UTM list into numpy array
list_utm_array = np.array(list_utm_rot, dtype = float)
#Create equal distance grid spacing
# Create x,y,z arrays for gridding
x = list_utm_array[:,0]
y = list_utm_array[:,1]
z = list_utm_array[:,2]
# Determine min and max x,y,z values
min_x = int(min(x))
max_x = int(max(x))
min_y = int(min(y))
max_y = int(max(y))
min_z = int(min(z))
max_z = int(max(z)) * 1.2 # multiply by 1.2 for greater interpolated z values
# Create empty x and y axes for meshgrid
xi = np.linspace(min_x, max_x, 100)
yi = np.linspace(min_y, max_y, 100)
# Create a empty meshgrid
X, Y = np.meshgrid(xi, yi)
# Interpolate rotated utm values onto the grid
# from scipy.interpolate import griddata
Z = griddata((x, y), z, (X, Y), method='cubic')
#Create contour plot using Matlibplot
contour_set = plt.contourf(X, Y, Z, levels = 5)
# Extract the paths of the contours from a contour plot
contours = plt.gca().collections[0].get_paths()
# Create GeoDataFrame from contours
contour_list = []
for contour in contours:
for polygon in contour.to_polygons():
contour_list.append(Polygon(polygon))
gdf = gpd.GeoDataFrame({'geometry': contour_list}, crs = "EPSG:32614")
# Save to shapefile
gdf.to_file("out.shp")
I would like to include the z values in the shape file so I can categorize the data in GIS. I have tried a few different ways with the following general code:
for i, collection in enumerate(contour_set.collections):
for path in collection.get_paths():
path = contour_set.get_paths()
vertices = path.vertices
polygon = Polygon(vertices)
z_value = contour_set.levels[i]
but I get the same error AttributeError: 'QuadContourSet' object has no attribute collections
which is my understanding because the Matlibplot code has been depreciated.
The code contours = plt.gca().collections[0].get_paths()
is supposed to replace the older collections format to extract the paths of the contours from a contour plot but am not sure how to extract the vertices and z values from the path using this new format and include in shape file. Any suggestions would be appreciated. Thanks.
I have successfully created a filled contour map with UTM data from a numpy array and exported to a shape file, but the shape file only has polygons with no other data. The partial code is:
#Convert UTM list into numpy array
list_utm_array = np.array(list_utm_rot, dtype = float)
#Create equal distance grid spacing
# Create x,y,z arrays for gridding
x = list_utm_array[:,0]
y = list_utm_array[:,1]
z = list_utm_array[:,2]
# Determine min and max x,y,z values
min_x = int(min(x))
max_x = int(max(x))
min_y = int(min(y))
max_y = int(max(y))
min_z = int(min(z))
max_z = int(max(z)) * 1.2 # multiply by 1.2 for greater interpolated z values
# Create empty x and y axes for meshgrid
xi = np.linspace(min_x, max_x, 100)
yi = np.linspace(min_y, max_y, 100)
# Create a empty meshgrid
X, Y = np.meshgrid(xi, yi)
# Interpolate rotated utm values onto the grid
# from scipy.interpolate import griddata
Z = griddata((x, y), z, (X, Y), method='cubic')
#Create contour plot using Matlibplot
contour_set = plt.contourf(X, Y, Z, levels = 5)
# Extract the paths of the contours from a contour plot
contours = plt.gca().collections[0].get_paths()
# Create GeoDataFrame from contours
contour_list = []
for contour in contours:
for polygon in contour.to_polygons():
contour_list.append(Polygon(polygon))
gdf = gpd.GeoDataFrame({'geometry': contour_list}, crs = "EPSG:32614")
# Save to shapefile
gdf.to_file("out.shp")
I would like to include the z values in the shape file so I can categorize the data in GIS. I have tried a few different ways with the following general code:
for i, collection in enumerate(contour_set.collections):
for path in collection.get_paths():
path = contour_set.get_paths()
vertices = path.vertices
polygon = Polygon(vertices)
z_value = contour_set.levels[i]
but I get the same error AttributeError: 'QuadContourSet' object has no attribute collections
which is my understanding because the Matlibplot code has been depreciated.
The code contours = plt.gca().collections[0].get_paths()
is supposed to replace the older collections format to extract the paths of the contours from a contour plot but am not sure how to extract the vertices and z values from the path using this new format and include in shape file. Any suggestions would be appreciated. Thanks.
1 Answer
Reset to default 0You’re only saving the geometry , but not the associated Z-levels from the contour.
So the shapefile has shapes, but no attribute data like elevation or contour value
gdf = gpd.GeoDataFrame({'geometry': contour_list}, crs = "EPSG:32614")
contour_list = []
level_list = []
for i, collection in enumerate(contour_set.collections):
level = contour_set.levels[i]
for path in collection.get_paths():
for polygon in path.to_polygons():
contour_list.append(Polygon(polygon))
level_list.append(level)
gdf = gpd.GeoDataFrame({'geometry': contour_list, 'level': level_list}, crs="EPSG:32614")
gdf.to_file("out.shp")
本文标签: geopandasHow export z values with shape fileStack Overflow
版权声明:本文标题:geopandas - How export z values with shape file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743975236a2570850.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论