-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextractShapefiles.py
executable file
·102 lines (82 loc) · 3.28 KB
/
extractShapefiles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2012 Instituto Nacional de Pesquisas Espaciais (INPE)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
import ogr
#Desired states
listStates = ['AC', 'AM', 'AP', 'MA', 'MT', 'PA', 'RO', 'RR', 'TO']
#Desired classes
listClasses = ['FLORESTA', 'HIDROGRAFIA']
#Loop in list of desired states
for desState in listStates:
#Loop in list of desired classes
for desClass in listClasses:
#Set the name to be openned
fileName = "/home/eduardo/ShapeFiles/PRODES/estados/PDigital2000_2011_"+desState+"_shp/PDigital2011_"+desState+"_pol.shp"
#Opens the shapefile and test the openning procedure
origFile = ogr.Open(fileName)
if origFile is None:
print "Open failed.\n"
print fileName
sys.exit(1)
#Set the new file to be written (initially a copy of the original)
destName = "/home/eduardo/ShapeFiles/PRODES/estados/PDigital2000_2011_"+desState+"_shp/new/"+desClass+"_PDigital2011_"+desState+"_pol.shp"
driveName = "ESRI Shapefile"
driver = ogr.GetDriverByName(driveName)
if driver is None:
print "Driver not available!\n"
sys.exit(1)
destFile = driver.CopyDataSource(origFile,destName)
#Close file
origFile = None
#Informs the user
print "File copied..."
print ""
#Reads the layer
layer = destFile.GetLayer()
print "Name of the layer: ", layer.GetName()
print ""
#Reads the layer definition
layer_def = layer.GetLayerDefn()
#Get the fields names
field_names = [layer_def.GetFieldDefn(i).GetName() for i in range (layer_def.GetFieldCount())]
print "Field names available: ", field_names
print ""
#Get the number of features
numberFeatures = layer.GetFeatureCount()
print "Number of features :", numberFeatures
print ""
#Let's count the number of desired features
counter = 0
for i in range(numberFeatures):
feature = layer.GetNextFeature()
classFeature = feature.GetFieldAsString(6)
if classFeature == desClass:
counter = counter + 1
else:
lfid = feature.GetFID()
errorCode = layer.DeleteFeature(lfid)
if errorCode != 0:
print "Error deleting feature.\n"
sys.exit(1)
feature.Destroy()
print "Number of new features: ", counter
#Repack SQL
destFile.ExecuteSQL("REPACK " + layer.GetName())
#Close file
destFile = None
#Program ends correctly
sys.exit(0)