基于Python的.tab文件转矢量文件(.shp)

请先使用txt查看器打开.tab文件查看以下重要信息

1. 寻找数据开始位置的分隔符(有的.tab在数据开始前会有一堆数据介绍,然后才是数据)

2. 查找你需要的列的名字和列位置

直接上代码

import pandas as pd
import geopandas as gpd
from shapely.geometry import Point

# Path to the input and output files
file_path = "I:\\Edge_Download\\GlobalCrowd.tab"
output_path = "J:\\Greening\\Human_impact\\HI.shp"

# Read the file line by line to find the start of the data
with open(file_path, 'r') as file:
    lines = file.readlines()

# Find the index of the line where the data starts
start_index = 0
for i, line in enumerate(lines):
    if line.startswith("*/"):#=========================寻找数据开始位置的分隔符
        start_index = i + 1
        break

# Read the data from the start_index
data = pd.read_csv(file_path, sep="\t", skiprows=start_index)

# Select the required columns
df = data[['Longitude', 'Latitude', 'HI [%] (Human Impact 1)', 'Conf']]#=======================需要数据的列名

# Filter rows where Conf is 0, 10, or 20
df_filtered = df[df['Conf'].isin([0, 10, 20])]

# Add a 'weight' column based on the 'Conf' values
df_filtered['weight'] = df_filtered['Conf'].apply(lambda x: 3 if x == 0 else (2 if x == 10 else 1))#=======请忽略这是我自己的增加列的需求

# Rename columns for convenience
df_filtered.rename(columns={'Longitude': 'lon', 'Latitude': 'lat', 'HI [%] (Human Impact 1)': 'HI'}, inplace=True)

# Create a GeoDataFrame
gdf = gpd.GeoDataFrame(df_filtered, geometry=gpd.points_from_xy(df_filtered.lon, df_filtered.lat))

# Set the coordinate reference system (CRS) to WGS84 (EPSG:4326)
gdf.set_crs(epsg=4326, inplace=True)

# Save the GeoDataFrame to a shapefile
gdf.to_file(output_path)

print(f"Shapefile saved to {output_path}")

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LHQ132

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值