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.

Share Improve this question asked Mar 30 at 20:48 RathprRathpr 35 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You’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