Python地图栅格化实例
引言
shapefile
是 GIS
中的一种非常重要的数据类型,由 ESRI
开发的空间数据开放格式,目前该数据格式已经成为了 GIS
领域的开放标准。目前绝大多数开源以及收费的 GIS
软件都支持该数据类型。事实上, shapefile
文件指的一种文件存储的方法,实际上该种文件是由多个文件组成的。组成一个 shapefile
有三种文件必不可少, ‘.shp’,’.shx’,’.dbf’文件。
geopandas
对 shapefile
提供了很好的读取与写出支持。 geopandas
库允许对几何类型进行空间操作,其中 DataFrame
结构相当于 GIS
数据中的一张属性表,使得可以直接操作矢量数据属性表,使得python中操作地理数据更加方便。本实例通过 geopandas
实现对地理数据的操作。
开发准备
由于 geopandas
库的安装需要一些前提库,因此需要先安装一些库
pip install pipwin
pipwin install gdal
pipwin install fiona
pip install geopandas
实测以上方法可以成功在windows下安装(注:如果在Anaconda下安装 geopandas
更为方便)
数据准备
该数据是一段GPS扫描数据,包含经纬度。
代码实例
环境引入
import geopandas as gp
import matplotlib.pyplot as plt
from shapely import geometry
import math
GPS数据处理
lake_original_path = 'data.txt'
lake_original_data = ''
lake_points = []
# 读取文件
with open(lake_original_path) as f:
lake_original_data = f.read()
# 处理经纬度坐标 并以Point的形式添加到list中
for xy in lake_original_data.split(';'):
x, _, y = xy.partition(',')
x = float(x.strip()) / 100
y = float(y.strip()) / 100
lake_points.append(geometry.Point(y, x))
创建要素
# 创建线状要素
lake_line = geometry.LineString(lake_points)
# crs指定坐标系
lake_ = gp.GeoSeries(lake_line, crs='EPSG:4326')
# 保存shp文件
lake_.to_file("boundary.shp", driver='ESRI Shapefile', encoding='utf-8')
# 记录边界条件 用于构建栅格
x_min, y_min, x_max, y_max = lake_line.bounds[:4]
# 绘图
lake_.plot()
plt.show()

构建栅格
# 栅格大小
GRID_WIDTH = 0.009 * 2 / 100
grid_rows_num = int(math.ceil((y_max - y_min) / float(GRID_WIDTH)))
grid_columns_num = int(math.ceil((x_max - x_min) / float(GRID_WIDTH)))
grids = []
for r in range(grid_rows_num):
for c in range(grid_columns_num):
grid_4coords = []
# 左上角
x_lt = x_min + c * GRID_WIDTH
y_lt = y_max - r * GRID_WIDTH
# 右上角
x_rt = x_lt + GRID_WIDTH
y_rt = y_lt
# 左下角
x_lb = x_lt
y_lb = y_lt - GRID_WIDTH
# 右下角
x_rb = x_rt
y_rb = y_lb
# 两个三角形拼接一个栅格
grid_4coords.append(geometry.Point(x_lt,y_lt))
grid_4coords.append(geometry.Point(x_rt,y_rt))
grid_4coords.append(geometry.Point(x_rb,y_rb))
grid_4coords.append(geometry.Point(x_lb,y_lb))
grid_4coords.append(geometry.Point(x_lt,y_lt))
# 创建一个网格
grids.append(geometry.LineString(grid_4coords))
grid_ = gp.GeoSeries(grids)
grid_.to_file('E:\\just\\海韵湖智能技术实验场\\data\\grids.shp',driver='ESRI Shapefile', encoding='utf-8')
grid_.plot()
plt.show()

要素叠加
# 要素叠加
elements = [lake_line]
elements += grids
elements_ = gp.GeoSeries(elements)
elements_.to_file('elements.shp', driver='ESRI Shapefile', encoding='utf-8')
elements_.plot()
plt.show()

参考链接
python-geopandas读取、创建shapefile文件、geopandas学习教程
Original: https://www.cnblogs.com/cnpolaris/p/16789522.html
Author: CNPolaris
Title: Python地图栅格化实例
相关阅读
Title: Django报错:ImportError: DLL load failed while importing _sqlite3: 找不到指定的模块
前言:
在PyCharm下使用Anaconda中的Python虚拟环境,创建Django项目时报错,其中Django是使用命令行 pip install Django
安装的,报错截图和代码如下:

创建 Django 应用程序时出错: Python 端错误。退出代码: 1,错误: Traceback (most recent call last):
File "C:\Users\suyin\Desktop\restaurant\manage.py", line 22, in <module>
main()
File "C:\Users\suyin\Desktop\restaurant\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\suyin\.conda\envs\restaurant\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
utility.execute()
File "C:\Users\suyin\.conda\envs\restaurant\lib\site-packages\django\core\management\__init__.py", line 395, in execute
django.setup()
File "C:\Users\suyin\.conda\envs\restaurant\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\suyin\.conda\envs\restaurant\lib\site-packages\django\apps\registry.py", line 114, in populate
app_config.import_models()
File "C:\Users\suyin\.conda\envs\restaurant\lib\site-packages\django\apps\config.py", line 301, in import_models
self.models_module = import_module(models_module_name)
File "C:\Users\suyin\.conda\envs\restaurant\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 855, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "C:\Users\suyin\.conda\envs\restaurant\lib\site-packages\django\contrib\auth\models.py", line 3, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "C:\Users\suyin\.conda\envs\restaurant\lib\site-packages\django\contrib\auth\base_user.py", line 48, in <module>
class AbstractBaseUser(models.Model):
File "C:\Users\suyin\.conda\envs\restaurant\lib\site-packages\django\db\models\base.py", line 122, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "C:\Users\suyin\.conda\envs\restaurant\lib\site-packages\django\db\models\base.py", line 326, in add_to_class
value.contribute_to_class(cls, name)
File "C:\Users\suyin\.conda\envs\restaurant\lib\site-packages\django\db\models\options.py", line 207, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "C:\Users\suyin\.conda\envs\restaurant\lib\site-packages\django\utils\connection.py", line 15, in __getattr__
return getattr(self._connections[self._alias], item)
File "C:\Users\suyin\.conda\envs\restaurant\lib\site-packages\django\utils\connection.py", line 62, in __getitem__
conn = self.create_connection(alias)
File "C:\Users\suyin\.conda\envs\restaurant\lib\site-packages\django\db\utils.py", line 204, in create_connection
backend = load_backend(db['ENGINE'])
File "C:\Users\suyin\.conda\envs\restaurant\lib\site-packages\django\db\utils.py", line 111, in load_backend
return import_module('%s.base' % backend_name)
File "C:\Users\suyin\.conda\envs\restaurant\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "C:\Users\suyin\.conda\envs\restaurant\lib\site-packages\django\db\backends\sqlite3\base.py", line 15, in <module>
from sqlite3 import dbapi2 as Database
File "C:\Users\suyin\.conda\envs\restaurant\lib\sqlite3\__init__.py", line 23, in <module>
from sqlite3.dbapi2 import *
File "C:\Users\suyin\.conda\envs\restaurant\lib\sqlite3\dbapi2.py", line 27, in <module>
from _sqlite3 import *
ImportError: DLL load failed while importing _sqlite3: 找不到指定的模块。
,输出:
</module></module></module></module></module></frozen></frozen></frozen></frozen></frozen></frozen></module>
环境
- Python版本:3.9.5
- Django版本:3.2.4
- conda版本:4.10.1
- Anaconda Navigator版本:2.0.3
报错原因
PyCharm在Anaconda虚拟环境下找不到sqlite3相关的模块
解决方案(步骤)
我这里以Windows 64位系统为例:
- 去SQLite官网下载sqlite3,如图:
- 解压文件
sqlite-dll-win64-x64-3360000.zip
- 将解压后的文件
sqlite3.def
、sqlite3.dll
复制到你Django项目所依赖的虚拟环境下的DLLs目录中,如图:
- 重启PyCharm,然后再创建Django项目即可
; 备用链接
下载sqlite3可能需要梯子,下载不下来的话可以用我这里下载好的
密码:15kn
Original: https://blog.csdn.net/qq_34562959/article/details/118283732
Author: 苏寅
Title: Django报错:ImportError: DLL load failed while importing _sqlite3: 找不到指定的模块
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/363567/
转载文章受原作者版权保护。转载请注明原作者出处!