溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Python 根據(jù)數(shù)據(jù)模板創(chuàng)建shapefile的實(shí)現(xiàn)

發(fā)布時(shí)間:2020-10-20 13:48:34 來(lái)源:腳本之家 閱讀:318 作者:staHuri 欄目:開(kāi)發(fā)技術(shù)

廢話不多說(shuō),我就直接上代碼讓大家看看吧!

#!/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"# 數(shù)據(jù)模板
out_shapefile = "shapefileAa.shp" # 輸出數(shù)據(jù)集
in_ds = ogr.Open(in_shapefile) # 讀取模板數(shù)據(jù)
in_lyr = in_ds.GetLayerByIndex(0)
if exists(out_shapefile):
  remove(out_shapefile)
drv = ogr.GetDriverByName("ESRI Shapefile") # 指定數(shù)據(jù)驅(qū)動(dòng)
out_ds = drv.CreateDataSource(out_shapefile) # 創(chuàng)建數(shù)據(jù)源
proj = in_lyr.GetSpatialRef() # 獲取模板坐標(biāo)系
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()): # 獲取字段長(zhǎng)度
  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)
  # 添加點(diǎn)
  out_lyr.CreateFeature(feature)
  # 關(guān)閉 特征
  feature = None
  # 關(guān)閉數(shù)據(jù)
data_source = None

以上這篇Python 根據(jù)數(shù)據(jù)模板創(chuàng)建shapefile的實(shí)現(xiàn)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI