Need help with the following code, The error comes at line 20.
import ogr
import os, shutil,sys
import osr
regionShp = "ne_50m_geography_regions_polys.shp"
regionFile = ogr.Open(regionShp)
regionLayer = regionFile.GetLayer(0)
spatRef = regionLayer.GetSpatialRef()
countryShp = "ne_50m_admin_0_countries.shp"
countryFile = ogr.Open(countryShp)
countryLayer = countryFile.GetLayer(0)
dstDir = "spatial_analysis_2"
if os.path.exists(dstDef):
shutil.rmtree(dstDir)
os.mddir(dstDir)
dstPath = os.path.join(dstDir, "china_region.shp"
Driver = ogr.GetDriverByName ("ESRI Shapefile")
detFile = driver.CreateDataSource(dstPath)
dstLayer = dstFile.CreateLayer("Layer",SpatRef)
fieldDef = ogr.FieldDefn ("regionFID", ogr.OFTInteger)
dstLayer.CreateField(fieldDef)
fieldDef = ogr.FieldDefn ("Name", ogr.OFTString)
fieldDef.SetWidth(50)
defLayer.CreateField(fieldDef)
fieldDef = ogr.FieldDefn("Class", ogr.OFTString)
fieldDef.SetWidth(50)
defLayer.CreateField(fieldDef)
countryFeature = countryLayer.GetNextFeature()
CountryName = countryFeature.GetField("name")
while countryName <> "china";
countryFeature = countryLayer.GetNextFeature()
countryName = countryFeature.FetField("Name")
china = countryFeature.GetGeometryRef()
regionLayer.SetSpatialFilter(China)
RegionFeature = regionLayer.GetNextFeature()
while regionFeature:
regionGeom = regionFeature.GetGeometryRef()
if china.Intersect(regionGeom):
intersect = regionGeom.Intersection(China)
dstFeature = ogr.Feature(dstLayer.GetLayerDefn())
dstFeature.GetField("regionFID", regionFeature.GetFID())
dstFeature.GetField("Name", regionFeature.GetField("name"))
dstFeature.GetField("Class", regionFeature.GetField("FeatureCla"))
dstFeature.SetGeomtry(Intersect)
print(str(dstFeature.GetField("regionFID")) + "," + dstFeature.GetField("Name" + "," + dstFeature.GetField("Class"))
dstLayer.CreateFeature(dstFeature)
try:
regionFeature = regionLayer.GetNextFeature()
except:
regionFeature = None
countryFile.Destroy()
regionFile.Destroy()
dstFile.Destroy()