shp数据解析(SHP文件格式解析与格式转换 - 知乎 (zhihu.com))
1. shp文件是什么(矢量数据格式)
- 用于存储和共享各种类型的gis数据,地图、地形、人口等内容。常见的shp文件由‘.shp’,‘.shx’,'.dbf’文件构成; ‘shp’包含实际的地理元素信息;‘shx’所指空间索引文件;‘dbf’文件表示包含属性信息;
- 通常由一系列有序文件组成:.shp、.shx、.dbf以及.prj文件。
- shp文件特点:
- SHP文件容易读取和使用(二进制文件)。
- 方便编辑。
- 适用于小规模矢量数据处理,可以在不同平台中转换和共享。
- 处理大规模数据和三位数据有局限,不支持三位数据的存储和显示。
- SHP的表现形式:
- 点状要素:以点的形式表示
- 线状要素:在地图上以线的形式表示,现状要素由一系列的坐标组成,即经纬度坐标。
- 生成shp文件
- SHP格式生成的方法有很多种,其中最常见的是使用AutoCAD或ArcGIS等软件来生成。
2. 如何生成geojson格式文件
- 通过python对shp文件进行生成geojson格式文件
- `import os
import geopandas as gpd
import pandas as pd
def convert_directory_to_geojson(input_directory, output_file):
“”"
Convert all GIS files in a directory to a single GeoJSON file.
Parameters:
- input_directory (str): Path to the directory containing the GIS files.
- output_file (str): Path to the output GeoJSON file.
"""
# 列出目录中的所有文件
files = [f for f in os.listdir(input_directory) if os.path.isfile(os.path.join(input_directory, f)) and f.endswith('.shp')]
# 创建一个空的GeoDataFrame列表来存储从每个文件中读取的数据
gdfs = []
for file in files:
filepath = os.path.join(input_directory, file)
gdf = gpd.read_file(filepath)
# 检查CRS,如果没有设置,则默认为WGS 84
if gdf.crs is None:
gdf.set_crs(epsg=4326, inplace=True) # WGS 84
gdfs.append(gdf)
# 将所有的GeoDataFrames合并为一个
combined_gdf = gpd.GeoDataFrame(pd.concat(gdfs, ignore_index=True))
# 转换为GeoJSON格式并保存
combined_gdf.to_file(output_file, driver="GeoJSON")
if __name__ == "__main__":
# 示例:将目录 'input_directory' 中的所有GIS文件转换为 'output.geojson'
convert_directory_to_geojson('GIS_File', 'output.geojson')`
3. 使用echarts添加geojson数据文件
-
首先对geojson数据进行压缩处理(https://www.sojson.com/)
-
对json数据进行格式化后,在js文件中显示:
将数据放在echarts.registerMap之中
4. 显示地图在echarts上
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="js/echarts.min.js"></script>
<script type="text/javascript" src="js/guizhou.js"></script>
<title></title>
</head>
<body>
<div id="main" style="width:100%;height:800px"></div>
</body>
<script>
var myChart = echarts.init(document.getElementById('main'));
var option = {
title: {
text : '贵州地图',
subtext : '各市区显示'
},
// 热力图
visualMap: {
//热力值范围
min: 40,
max: 50000,
//热力值的颜色与文字
text: ['High', 'Low'],
color: ['red', 'blue'],
//实时显示
realtime: true,
//计算
calculable: true,
//颜色显示范围
inRange: {
color: ['lightskyblue', 'yellow', 'orangered']
},
textStyle: {
//文字大小
fontSize: 18,
// 文字类型
fontWeight: 'bolder',
color: 'red' // 主标题文字颜色
},
},
series: [{
name: '数据名称',
type: 'map',
roam:true,
mapType: 'guizhou',
selectedMode : 'single',
itemStyle:{
normal:{label:{show:true}},
emphasis:{label:{show:true}}
},
data:[
{name: '贵阳市',value: 20057.34},
{name: '遵义市',value: 15477.48},
{name: '六盘水市',value: 31686.1},
{name: '毕节市',value: 16992.6},
{name: '铜仁市',value: 44045.49},
{name: '安顺市',value: 40689.64},
{name: '黔西南布依族苗族自治州',value: 37659.78},
{name: '黔东南苗族侗族自治州', value: 45180.97},
{name: '黔南布依族苗族自治州',value: 55204.26},
],
}]
};
myChart.setOption(option);
</script>
</html>