Land use and land cover are important concepts in environmental science and urban planning, and they can help this article understand the relationship between man and nature and promote sustainable development. With the acceleration of urbanization, it is particularly important to scientifically monitor and manage land resources. As a powerful programming language, Python is widely used in this work with its rich data analysis library. This article will provide detailed code examples of how to map land use and land cover type using Python.
1. Installation of required libraries
First, this article needs to install the Python library required to map land use and land cover type maps. These libraries include pandas, matplotlib, geopandas, etc. You can use the following command to install:
pip install pandas geopandas matplotlib
2. Data preparation
In order to map land use and land cover type maps, this paper needs to prepare corresponding geographical data. These data are usually stored in GeoJSON, Shapefile and other formats. In the examples in this article, this article will use data in GeoJSON format.
Suppose there is a name in this articleland_use.geojson
file containing data on land use type. This document can be obtained through various geodata platforms or government agencies.
3. Draw land use and land cover type map
Here is a complete code example showing how to map land use and land cover type using Python:
import geopandas as gpd import as plt # Read geographic data, you can use GeoJSON, Shapefile and other formatsdata = gpd.read_file('land_use.geojson') # Confirm that the data is loading successfully and print the first 5 linesprint(()) # Set drawing stylefig, ax = (1, 1, figsize=(12, 10)) # Map land use and cover type(column='land_use_type', ax=ax, legend=True, legend_kwds={'label': "Land Use Types", 'orientation': "horizontal"}, cmap='Set3') # Add a chart titleax.set_title('Land Use and Land Cover Types Map', fontsize=15) ax.set_xlabel('Longitude', fontsize=12) ax.set_ylabel('Latitude', fontsize=12) # Show map()
4. Code explanation
Import library:
import geopandas as gpd import as plt
This article imports geopandas and matplotlib libraries. geopandas is used to read and process geographic data, and matplotlib is used to plot.
Read geographic data:
data = gpd.read_file('land_use.geojson')
Using geopandasread_file
Methods read geodata files in GeoJSON format.
Confirm the data loading successfully:
print(())
Print the first 5 lines of the data to ensure the data loads successfully and understand the basic structure of the data.
Set drawing style:
fig, ax = (1, 1, figsize=(12, 10))
Using matplotlibsubplots
Method creates a drawing object and an axis object and sets the size of the image.
Map land use and cover type:
(column='land_use_type', ax=ax, legend=True, legend_kwds={'label': "Land Use Types", 'orientation': "horizontal"}, cmap='Set3')
Using geopandasplot
Methods The data are plotted on the map according to different land use types.column
Parameters specify the column name to be drawn.ax
Parameters specify the axis object,legend
Parameters indicate whether to display legend.legend_kwds
Parameters are used to style the legend.cmap
Parameters are used to set the color map.
Add chart title and axis labels:
ax.set_title('Land Use and Land Cover Types Map', fontsize=15) ax.set_xlabel('Longitude', fontsize=12) ax.set_ylabel('Latitude', fontsize=12)
useset_title
Method to set the chart title, useset_xlabel
andset_ylabel
Method Set the axis label.
Show map:
()
Using matplotlibshow
Method to display the map.
V. Other visual forms
In addition to mapping land use and land cover type maps, this article can also use Python to draw other types of charts to more comprehensively show data on land use and land cover.
1. Pie bar chart
A pie chart is a graph for displaying the proportional relationship of each part relative to the whole. Here is an example of using matplotlib to draw a pie chart of land use types:
import pandas as pd import as plt # Sample Datasetdata = { 'Land use type': ['forest', 'farmland', 'City', 'Body of Water', 'grassland'], 'area(Square km)': [150, 320, 100, 50, 80] } df = (data) df['Area ratio'] = df['area(Square km)'] / df['area(Square km)'].sum() # Draw a pie chart(figsize=(8, 6)) (df['Area ratio'], labels=df['Land use type'], autopct='%.1f%%', startangle=140) ('Land use type distribution') ('equal') # Make the pie chart a circle()
2. Bar chart
A bar chart is a graph used to show the distribution of different categories of data. Here is an example of plotting a histogram of land cover types using matplotlib:
import pandas as pd import as plt # Load land cover datadata = pd.read_csv('land_cover_data.csv') # Statistics the number of different surface coverage typescover_types = data['cover_type'].value_counts() # Draw a bar chart(figsize=(10, 6)) (cover_types.index, cover_types.values) ('Land Cover Distribution') ('Cover Type') ('Count') ()
3. Sankito
A Sanki diagram is a graph used to show a flow or transformation process. Here is an example of plotly plotting the Sanki map of land use transfers:
Here is a complete example of supplementing the Sanki Diagram code:
import plotly.graph_objects as go # Define the transfer matrixtransfer_matrix_2014_to_2024= [ [383.69, 3.51, 0.02, 7.19, 4.27, 0.24], [8.66, 166.07, 1.44, 63.02, 17.73, 39.24], [0, 0.88, 3.73, 0.09, 0.01, 0.23], [12.76, 29.42, 1.1, 79.35, 11.97, 25.42], [0.27, 3.73, 0.03, 1.64, 1.72, 0.94], [2.14, 4.39, 0.05, 11.69, 1.15, 2.75] ] # Category Namecategories = ["woodland", "Construction land", "Both of Water", "arable land", "Unused Land", "grassland"] # Create node (start node and target node)labels = [] ([f"2014_{category}" for category in categories]) ([f"2024_{category}" for category in categories]) # Create source and targetsource = [] target = [] value = [] # Transfer data from 2014 to 2024for i in range(len(categories)): for j in range(len(categories)): if transfer_matrix_2014_to_2024[i][j] > 0: (i) # From the 2014 category (len(categories) + j) # Categories by 2024 (transfer_matrix_2014_to_2024[i][j]) # Define colorcolors = [ "#228B22", # Woodland (dark green) "#D2691E", # Construction land (deep orange) "#1E90FF", # Water (bright blue) "#FFD700", # Cultivated Land (Golden) "#A9A9A9", #Unutilised (dark gray) "#8FBC8F" # Grass (Green)] # Assign colors to each node (the first half is from 2014, the second half is from 2024, the colors are the same)node_colors = colors * 2 # Create a Sankiemafig = (data=[( node=dict( pad=15, # Spacing between nodes thickness=20, line=dict(color="black", width=0.5), label=labels, color=node_colors ), link=dict( source=source, target=target, value=value ))]) # Set up the layoutfig.update_layout(title_text="Sankitu, Land Use Type Transfer from 2014 to 2024", font_size=10) # Show charts()
In this code, this article usesplotly.graph_objects
library to create a Sanki map. This article defines the transfer matrixtransfer_matrix_2014_to_2024
, it indicates the amount of transfers between various land use types from 2014 to 2024. Then, this article creates the node taglabels
,source
andtarget
list to store the start and end nodes of the link, andvalue
List to store the value of the link.
This article also defines colors for each land use type and assigns these colors to nodes. Finally, this article usesThe function creates a Sankey diagram and passes
fig.update_layout
Set the layout and title of the chart. Finally, use()
Show the chart.
Please make sure you have installed itplotly
The library can be installed using the following command:
pip install plotly
After running the above code, you will see an interactive Sanki graph showing the transfer between various land use types from 2014 to 2024.
6. Summary
In the field of data visualization, Python has become one of the preferred languages for processing and analyzing geospatial data with its powerful libraries and toolsets. Especially when this article needs to map land use and land cover type maps, Python provides rich libraries such as Plotly, Matplotlib, Geopandas, etc. to help this article effectively complete this task. This article focuses on how to use the Plotly library to map the Sankey map of land use transfers. Sankima is a graph designed specifically for displaying flow or conversion processes, which is ideal for representing the transfer of land use types between different time periods. Through Python and Plotly libraries, this article can easily create a Sankey map of land use transfers to visually show the transfer of land use types between different time periods. This approach not only improves the efficiency of data visualization, but also enhances the readability and interactivity of the chart. Whether it is scientific researchers, data analysts or geospatial information professionals, they can better understand and analyze data on land use and land cover changes by mastering this approach.
This is the end of this article about the detailed explanation of Python's example of land use and land cover type maps. For more related contents of Python's land use and land cover type maps, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!