Convert the latitude and longitude point coordinate files stored in CSV format into vector data of point
type and line
type of shapefile, mainly relying on the geopandas and shapely packages.
Use the Point spatial data model in shapely to build a Point
object, and pass it as a parameter to the geometry
property of GeoDataFrame
, one of the main types of GeoPandas. In the creation of line features, the LineString
spatial data model of shapely
needs to be used, and the LineString
object is constructed and passed into the geodataframe
. The complete code is as follows.
# Convert to point features, the latitude and longitude coordinate format is wgslng, wgslat
import os
import pandas as pd
import geopandas as gpd
from shapely.geometry import Point
# CSV to shapefile data
def csv_to_points():
input_path = "E:\\Data\\csv\\"
output_path = "E:\\Data\\shp\\"
for file in os.listdir(input_path):
df = pd.read_csv(input_path+file, header=0, encoding='gbk')
geometry = [Point(xy) for xy in zip(df.wgslng, df.wgslat)] # Need to be modified to the corresponding latitude and longitude fields
gdf = gpd.GeoDataFrame(df, crs="EPSG:4326", geometry=geometry) # Specify the coordinate system
gdf.to_file(output_path+file[0:-4]+".shp", encoding='gbk')
print('finished------' + file)
print('finished------' )
import pandas as pd
import geopandas as gpd
from shapely.geometry import LineString
# CSV is converted to shapefile line data, the data format is lng_oo, lat_oo, lng_dd, lat_dd
def csv_to_lines():
input_path = "E:\\A Bite of China\\Data\\Flow\\"
output_path = "E:\\A Bite of China\\Data\\Flow\\shp\\"
file = "flow.csv"
df = pd.read_csv(input_path + file, header=0, encoding='gbk')
geometry = [LineString(xy_list) for xy_list in zip(zip(df.lng_oo, df.lat_oo), zip(df.lng_dd, df.lat_dd))]
gdf = gpd.GeoDataFrame(df, crs="EPSG:4326", geometry=geometry)
gdf.to_file(output_path + "flow.shp", encoding='gbk')
print('finished------' )