Python 根據數據模板創(chuàng)建shapefile的實現
更新時間:2019年11月26日 11:16:15 作者:staHuri
今天小編就為大家分享一篇Python 根據數據模板創(chuàng)建shapefile的實現,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
廢話不多說,我就直接上代碼讓大家看看吧!
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File : copyShapefile.py
# @Author: huifer
# @Date : 2018-4-28
from os.path import exists
import gdal
from osgeo import ogr
from os import remove
gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES") # 路徑中文
gdal.SetConfigOption("SHAPE_ENCODING", "GBK") # 屬性中文
in_shapefile = "dataSample/wang_point.shp"# 數據模板
out_shapefile = "shapefileAa.shp" # 輸出數據集
in_ds = ogr.Open(in_shapefile) # 讀取模板數據
in_lyr = in_ds.GetLayerByIndex(0)
if exists(out_shapefile):
remove(out_shapefile)
drv = ogr.GetDriverByName("ESRI Shapefile") # 指定數據驅動
out_ds = drv.CreateDataSource(out_shapefile) # 創(chuàng)建數據源
proj = in_lyr.GetSpatialRef() # 獲取模板坐標系
out_lyr = out_ds.CreateLayer(out_shapefile.split(".")[0], proj, ogr.wkbPoint)
# copy the schema of the original shapefile to the destination shapefile
lyr_def = in_lyr.GetLayerDefn()
for i in range(lyr_def.GetFieldCount()): # 獲取字段長度
out_lyr.CreateField(lyr_def.GetFieldDefn(i)) # 創(chuàng)建字段
feature = ogr.Feature(lyr_def)
wkt = "POINT(88615.730000 75345.486000)"
point = ogr.CreateGeometryFromWkt(wkt)
feature.SetGeometry(point)
# 添加點
out_lyr.CreateFeature(feature)
# 關閉 特征
feature = None
# 關閉數據
data_source = None
以上這篇Python 根據數據模板創(chuàng)建shapefile的實現就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Python OpenCV實戰(zhàn)之與機器學習的碰撞
機器學習是人工智能的子集,為計算機以及其它具有計算能力的系統(tǒng)提供自動預測或決策的能力。本文主要介紹了OpenCV 提供的常見機器學習算法和技術,用于解決計算機視覺項目中的實際問題,需要的朋友可以參考一下2021-12-12
python?pyvis庫創(chuàng)建可視化交互式網絡圖
這篇文章主要為大家介紹了python?pyvis庫創(chuàng)建可視化交互式網絡圖,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01
Python 利用scrapy爬蟲通過短短50行代碼下載整站短視頻
近日,有朋友向我求助一件小事兒,他在一個短視頻app上看到一個好玩兒的段子,想下載下來,可死活找不到下載的方法。經過我的一番研究才找到解決方法,下面小編給大家分享Python 利用scrapy爬蟲通過短短50行代碼下載整站短視頻的方法,感興趣的朋友一起看看吧2018-10-10

