当前位置:网站首页>GDAL + ogr learning

GDAL + ogr learning

2022-04-23 18:00:00 Be happy to study today

GDAL+OGR Study

Every time you write code, use Arcpy You have to switch back python2, so much trouble , So let's learn gdal+ogr.

gdal Libraries installed

direct cmd Write pip intsall gdal Will report a mistake
 Insert picture description here
Open the link gdal Library Download , according to python Version download the package of the corresponding version , My version is python3.9+64 position , Download on demand .
 Insert picture description here
 Insert picture description here
take wheel Copy the file to the corresponding folder and run again pip install Installation successful . Insert picture description here

Use OGR Filter data

They are Gaud POI surface type The structure and POIshp type Structure
 Insert picture description here  Insert picture description here
Will use the... Contained in the form POI The type is right shp Inside POI Type

#-*- coding:utf-8 -*-
#2022.2.10
'''
 Put the statistics in the table poi Screening , Then select... In the designated area poi Category 
'''
import time
import pandas as pd
from osgeo import ogr
import os

def identify(poistr,selectpoilist):
    if poistr.split(';')[2] in selectpoilist:
        return 1
    else:
        return 0

def selectpoi(poitype,lyr):
    n = lyr.GetFeatureCount()
    print(" common ",n," strip poi data ")
    # Add fields 
    defn = lyr.GetLayerDefn()
    fieldIndex = defn.GetFieldIndex('selectid')
    if fieldIndex < 0:
        #  Add fields 
        lyr.CreateField(  #  Add a layer selectid Field 
            ogr.FieldDefn('selectid', ogr.OFTInteger,SetPrecision(3))
        )
    fieldIndex2 = defn.GetFieldIndex('selectid')
    if fieldIndex2 > 0:
        print(" Field created successfully :", fieldIndex)
      
    m = 0
    starttime = time.time()
    for feat in lyr:
        name = feat.GetField('name')
        type = feat.GetField('type')
        # Calculation r Value to determine whether to select ,>0 Then choose , yes 0 You don't choose 
        if (type.count("|")>0):
            r=0
            for i in range(0,type.count("|")):
                r =r+identify(type.split('|')[i],poitype)
        else:
            r=identify(type,poitype)
        if  r>0:
            feat.SetField('selectid', 1)  #  Set the value of the field 
        else:
            feat.SetField('selectid', 0)  #  Set the value of the field 
        lyr.SetFeature(feat)
        m=m+1
        print(" Running page ",m," Data ",feat.GetField('selectid'),name,type)
        if m==n:
            break;
    endtime = time.time()
    print(" Total running time ",endtime-starttime)


if  __name__=="__main__":
    # Read POI Type table 
    datapath = r'E:\WHDATA\region_data\jianghan_district\database'
    poifilename  = "./ Sub category POI.xlsx"
    poitype = pd.read_excel(poifilename)
    type = poitype[' Subclass '].to_list()
    # Read Jianghan District POIshp data 
    in_poishp = os.path.join(datapath,"poi.shp")
    outpath = os.path.join(datapath,"poi_select.shp")
    ds = ogr.Open(in_poishp,1)#1 Said to write ,0 Indicates read-only 
    lyr = ds.GetLayer()
    selectpoi(type,lyr)
    

版权声明
本文为[Be happy to study today]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230545528278.html