source: grass-addons/grass7/vector/v.in.natura2000/v.in.natura2000.py

Last change on this file was 73971, checked in by neteler, 6 years ago

vector addons: Python3 compatibility changes, using 2to3 from python-tools

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-python
File size: 16.3 KB
Line 
1#!/usr/bin/env python
2
3"""
4MODULE: v.in.natura2000
5
6AUTHOR(S): Helmut Kudrnovsky <alectoria AT gmx at>
7
8PURPOSE: Imports Natura 2000 spatial data of protected areas
9
10COPYRIGHT: (C) 2015 by the GRASS Development Team
11
12 This program is free software under the GNU General Public
13 License (>=v2). Read the file COPYING that comes with GRASS
14 for details.
15"""
16
17#%module
18#% description: importing of Natura 2000 spatial data of protected areas
19#% keyword: vector
20#% keyword: geometry
21#%end
22
23#%option G_OPT_F_BIN_INPUT
24#% key: input
25#% required: yes
26#%end
27
28#%option G_OPT_V_OUTPUT
29#% key: output
30#% description: name of imported spatial data set
31#% required : no
32#% guisection: output
33#%end
34
35#%option sitetype
36#% key: sitetype
37#% description: Select site type of input (A, B or C)
38#% required : no
39#% guisection: selection
40#%end
41
42#%option habitat_code
43#% key: habitat_code
44#% description: Select habitat code of input
45#% required : no
46#% guisection: selection
47#%end
48
49#%option species_code
50#% key: species_code
51#% description: Select species of input
52#% required : no
53#% guisection: selection
54#%end
55
56#%option biogeographic_region
57#% key: biogeographic_region
58#% description: Select biogeographic region of input
59#% required : no
60#% guisection: selection
61#%end
62
63#%option member_state
64#% key: member_state
65#% description: Select member state of input
66#% required : no
67#% guisection: selection
68#%end
69
70#%option existing_layer
71#% key: existing_layer
72#% description: Import of existing layer
73#% required : no
74#% guisection: layer
75#%end
76
77#%flag
78#% key: l
79#% description: Print available layer
80#% guisection: layer
81#%end
82
83#%flag
84#% key: b
85#% description: Print list of biogeographic regions
86#% guisection: print
87#%end
88
89#%flag
90#% key: m
91#% description: Print list of EU member states codes
92#% guisection: print
93#%end
94
95#%flag
96#% key: h
97#% description: Print list of habitats of community interest
98#% guisection: print
99#%end
100
101#%flag
102#% key: s
103#% description: Print list of species of community interest
104#% guisection: print
105#%end
106
107#%flag
108#% key: t
109#% description: Print list of protected area site types
110#% guisection: print
111#%end
112
113import sys
114import os
115import csv
116import math
117import shutil
118import tempfile
119import grass.script as grass
120
121
122def main():
123
124 n2k_input = options['input']
125 n2k_output = options['output']
126 pa_sitetype_input = options['sitetype']
127 habitat_code_input = options['habitat_code']
128 species_code_input = options['species_code']
129 biogeoreg_long = options['biogeographic_region']
130 biogeoreg_long2 = biogeoreg_long.replace(" ", "_")
131 biogeoreg_long_quoted = '"'+biogeoreg_long+'"'
132 habitat_view = 'v'+habitat_code_input
133 habitat_spatial_view = 'sv'+habitat_code_input
134 species_view = 'v'+species_code_input
135 species_spatial_view = 'sv'+species_code_input
136 biogeoreg_view = 'v'+biogeoreg_long2
137 biogeoreg_spatial_view = 'sv'+biogeoreg_long2
138 ms_input = options['member_state']
139 layer_exist = options['existing_layer']
140 list_n2k_layer = flags['l']
141 list_bg_reg = flags['b']
142 list_ms = flags['m']
143 list_habitats = flags['h']
144 list_species = flags['s']
145 list_site_type = flags['t']
146 global tmp
147
148 try:
149 import pyspatialite.dbapi2 as db
150 except:
151 grass.fatal( "pyspatialite is needed to run this script.\n"
152 "source: https://pypi.python.org/pypi/pyspatialite \n"
153 "Please activate/install it in your python stack.")
154
155 if list_n2k_layer :
156 grass.message( "Available data layer(s):" )
157 grass.message( "may take some time ..." )
158 grass.message( "..." )
159 grass.run_command("v.in.ogr", input = n2k_input,
160 flags = 'l')
161
162 if list_bg_reg :
163 grass.message( "Biogeographic regions:" )
164 conn = db.connect("%s" % n2k_input)
165 c = conn.cursor()
166 for row in c.execute('SELECT BIOGEFRAPHICREG FROM BIOREGION GROUP BY BIOGEFRAPHICREG'):
167 grass.message( row )
168 conn.close()
169
170 if list_ms :
171 grass.message( "EU member states:" )
172 conn = db.connect("%s" % n2k_input)
173 c = conn.cursor()
174 for row in c.execute('SELECT MS FROM Natura2000polygon GROUP BY MS'):
175 grass.message( row )
176 conn.close()
177
178 if list_habitats :
179 grass.message( "habitat codes of EU community interest:" )
180 conn = db.connect("%s" % n2k_input)
181 c = conn.cursor()
182 try:
183 for row in c.execute('SELECT HABITATCODE, DESCRIPTION FROM HABITATS GROUP BY HABITATCODE'):
184 grass.message( row )
185 except:
186 pass
187 grass.warning("Some problems querying habitat code or names occurred."
188 " Please check columns HABITATCODE and DESCRIPTION of table HABITATS in the sqlite database.")
189 conn.close()
190
191 if list_species :
192 grass.message( "species codes of EU community interest:" )
193 conn = db.connect("%s" % n2k_input)
194 c = conn.cursor()
195 try:
196 for row in c.execute('SELECT SPECIESCODE, SPECIESNAME FROM SPECIES GROUP BY SPECIESCODE'):
197 grass.message( row )
198 except:
199 pass
200 grass.warning("Some problems querying species code or names occurred."
201 " Please check columns SPECIESCODE and SPECIESNAME of table SPECIES in the sqlite database.")
202 conn.close()
203
204 if list_site_type :
205 grass.message( "site types:" )
206 conn = db.connect("%s" % n2k_input)
207 c = conn.cursor()
208 for row in c.execute('SELECT SITETYPE FROM NATURA2000SITES GROUP BY SITETYPE'):
209 grass.message( row )
210 conn.close()
211
212 if pa_sitetype_input :
213 grass.message( "importing protected areas of site type: %s" % pa_sitetype_input )
214 grass.message( "may take some time ..." )
215 grass.run_command( "v.in.ogr", input = "%s" % (n2k_input),
216 layer = "natura2000polygon",
217 output = n2k_output,
218 where = "SITETYPE = '%s'" % (pa_sitetype_input),
219 quiet = False)
220
221 if ms_input :
222 grass.message( "importing protected areas of member state: %s" % ms_input )
223 grass.message( "may take some time ..." )
224 grass.run_command( "v.in.ogr", input = "%s" % (n2k_input),
225 layer = "natura2000polygon",
226 output = n2k_output,
227 where = "MS = '%s'" % (ms_input),
228 quiet = False)
229
230 if habitat_code_input :
231 grass.message( "importing protected areas with habitat (code): %s" % habitat_code_input )
232 grass.message( "preparing (spatial) views in the sqlite/spatialite database:" )
233 conn = db.connect("%s" % n2k_input)
234 c = conn.cursor()
235 # create view of defined habitat
236 grass.message( "view: %s" % habitat_view )
237 sqlhabitat = 'CREATE VIEW "%s" AS ' % (habitat_view)
238 sqlhabitat += 'SELECT * FROM HABITATS '
239 sqlhabitat += 'WHERE HABITATCODE = "%s" ' % (habitat_code_input)
240 sqlhabitat += 'ORDER BY "SITECODE"'
241 grass.message ( sqlhabitat )
242 c.execute( sqlhabitat )
243 # create spatial view of defined habitat - part 1
244 grass.message( "spatial view: %s" % habitat_spatial_view )
245 sqlhabitatspatial1 = 'CREATE VIEW "%s" AS ' % (habitat_spatial_view)
246 sqlhabitatspatial1 += 'SELECT "a"."ROWID" AS "ROWID", "a"."PK_UID" AS "PK_UID", '
247 sqlhabitatspatial1 += '"a"."SITECODE" AS "SITECODE", "a"."SITENAME" AS "SITENAME", '
248 sqlhabitatspatial1 += '"a"."RELEASE_DA" AS "RELEASE_DA", "a"."MS" AS "MS", '
249 sqlhabitatspatial1 += '"a"."SITETYPE" AS "SITETYPE", "a"."Geometry" AS "Geometry", '
250 sqlhabitatspatial1 += '"b"."SITECODE" AS "SITECODE_1", "b"."HABITATCODE" AS "HABITATCODE", "b"."DESCRIPTION" AS "DESCRIPTION", '
251 sqlhabitatspatial1 += '"b"."COVER_HA" AS "COVER_HA", "b"."CAVES" AS "CAVES", "b"."REPRESENTATIVITY" AS "REPRESENTATIVITY", '
252 sqlhabitatspatial1 += '"b"."RELSURFACE" AS "RELSURFACE", "b"."CONSERVATION" AS "CONSERVATION", '
253 sqlhabitatspatial1 += '"b"."GLOBAL_ASSESMENT" AS "GLOBAL_ASSESMENT", "b"."DATAQUALITY" AS "DATAQUALITY", '
254 sqlhabitatspatial1 += '"b"."PERCENTAGECOVER" AS "PERCENTAGECOVER" '
255 sqlhabitatspatial1 += 'FROM "Natura2000polygon" AS "a" '
256 sqlhabitatspatial1 += 'JOIN %s AS "b" USING ("SITECODE") ' % (habitat_view)
257 sqlhabitatspatial1 += 'ORDER BY "a"."SITECODE";'
258 grass.message ( sqlhabitatspatial1 )
259 c.execute( sqlhabitatspatial1 )
260 # create spatial view of defined habitat - part 2
261 sqlhabitatspatial2 = 'INSERT INTO views_geometry_columns '
262 sqlhabitatspatial2 += '(view_name, view_geometry, view_rowid, f_table_name, f_geometry_column, read_only) '
263 sqlhabitatspatial2 += 'VALUES ("%s", "geometry", "rowid", "natura2000polygon", "geometry", 1);' % (habitat_spatial_view.lower())
264 grass.message ( sqlhabitatspatial2 )
265 # execute spatial vieww
266 c.execute( sqlhabitatspatial2 )
267 conn.commit()
268 conn.close()
269 # import spatial view
270 grass.message ( "importing data..." )
271 grass.message ( "may take some time..." )
272 grass.run_command( "v.in.ogr", input = "%s" % (n2k_input),
273 layer = "%s" % (habitat_spatial_view),
274 output = n2k_output,
275 quiet = False)
276
277 if species_code_input :
278 grass.message( "importing protected areas with species (code): %s" % species_code_input )
279 grass.message( "preparing (spatial) views in the sqlite/spatialite database:" )
280 conn = db.connect("%s" % n2k_input)
281 c = conn.cursor()
282 # create view of defined species
283 grass.message( "view: %s" % species_view )
284 sqlspecies = 'CREATE VIEW "%s" AS ' % (species_view)
285 sqlspecies += 'SELECT * FROM SPECIES '
286 sqlspecies += 'WHERE SPECIESCODE = "%s" ' % (species_code_input)
287 sqlspecies += 'ORDER BY "SITECODE"'
288 grass.message ( sqlspecies )
289 c.execute( sqlspecies )
290 # create spatial view of defined species - part 1
291 grass.message( "spatial view: %s" % species_spatial_view )
292 sqlspeciesspatial1 = 'CREATE VIEW "%s" AS ' % (species_spatial_view)
293 sqlspeciesspatial1 += 'SELECT "a"."ROWID" AS "ROWID", "a"."PK_UID" AS "PK_UID", '
294 sqlspeciesspatial1 += '"a"."SITECODE" AS "SITECODE", "a"."SITENAME" AS "SITENAME", '
295 sqlspeciesspatial1 += '"a"."RELEASE_DA" AS "RELEASE_DA", "a"."MS" AS "MS", '
296 sqlspeciesspatial1 += '"a"."SITETYPE" AS "SITETYPE", "a"."Geometry" AS "Geometry", '
297 sqlspeciesspatial1 += '"b"."COUNTRY_CODE" AS "COUNTRY_CODE", "b"."SITECODE" AS "SITECODE_1", '
298 sqlspeciesspatial1 += '"b"."SPECIESNAME" AS "SPECIESNAME", "b"."SPECIESCODE" AS "SPECIESCODE", '
299 sqlspeciesspatial1 += '"b"."REF_SPGROUP" AS "REF_SPGROUP", "b"."SPGROUP" AS "SPGROUP", '
300 sqlspeciesspatial1 += '"b"."SENSITIVE" AS "SENSITIVE", "b"."NONPRESENCEINSITE" AS "NONPRESENCEINSITE", '
301 sqlspeciesspatial1 += '"b"."POPULATION_TYPE" AS "POPULATION_TYPE", "b"."LOWERBOUND" AS "LOWERBOUND", '
302 sqlspeciesspatial1 += '"b"."UPPERBOUND" AS "UPPERBOUND", "b"."COUNTING_UNIT" AS "COUNTING_UNIT", '
303 sqlspeciesspatial1 += '"b"."ABUNDANCE_CATEGORY" AS "ABUNDANCE_CATEGORY", '
304 sqlspeciesspatial1 += '"b"."DATAQUALITY" AS "DATAQUALITY", "b"."POPULATION" AS "POPULATION", '
305 sqlspeciesspatial1 += '"b"."CONSERVATION" AS "CONSERVATION", "b"."ISOLATION" AS "ISOLATION", '
306 sqlspeciesspatial1 += '"b"."GLOBAL" AS "GLOBAL" '
307 sqlspeciesspatial1 += 'FROM "Natura2000polygon" AS "a" '
308 sqlspeciesspatial1 += 'JOIN %s AS "b" USING ("SITECODE") ' % (species_view)
309 sqlspeciesspatial1 += 'ORDER BY "a"."SITECODE";'
310 grass.message ( sqlspeciesspatial1 )
311 c.execute( sqlspeciesspatial1 )
312 # create spatial view of defined habitat - part 2
313 sqlspeciesspatial2 = 'INSERT INTO views_geometry_columns '
314 sqlspeciesspatial2 += '(view_name, view_geometry, view_rowid, f_table_name, f_geometry_column, read_only) '
315 sqlspeciesspatial2 += 'VALUES ("%s", "geometry", "rowid", "natura2000polygon", "geometry", 1);' % (species_spatial_view.lower())
316 grass.message ( sqlspeciesspatial2 )
317 # execute spatial view
318 c.execute( sqlspeciesspatial2 )
319 conn.commit()
320 conn.close()
321 # import spatial view
322 grass.message ( "importing data..." )
323 grass.message ( "may take some time..." )
324 grass.run_command( "v.in.ogr", input = "%s" % (n2k_input),
325 layer = "%s" % (species_spatial_view),
326 output = n2k_output,
327 quiet = False)
328
329 if biogeoreg_long :
330 grass.message( "importing protected areas of biogeographic region: %s" % biogeoreg_long )
331 grass.message( "preparing (spatial) views in the sqlite/spatialite database:" )
332 conn = db.connect("%s" % n2k_input)
333 c = conn.cursor()
334 # create view of defined biogeographic region
335 grass.message( "view: %s" % biogeoreg_view )
336 sqlbioreg = 'CREATE VIEW "%s" AS ' % (biogeoreg_view)
337 sqlbioreg += 'SELECT * FROM BIOREGION '
338 sqlbioreg += 'WHERE BIOGEFRAPHICREG = "%s" ' % (biogeoreg_long)
339 sqlbioreg += 'ORDER BY "SITECODE"'
340 grass.message ( sqlbioreg )
341 c.execute( sqlbioreg )
342 # create spatial view of defined biogeographical region - part 1
343 grass.message( "spatial view: %s" % biogeoreg_spatial_view )
344 sqlbioregspatial1 = 'CREATE VIEW "%s" AS ' % (biogeoreg_spatial_view)
345 sqlbioregspatial1 += 'SELECT "a"."ROWID" AS "ROWID", "a"."PK_UID" AS "PK_UID", '
346 sqlbioregspatial1 += '"a"."RELEASE_DA" AS "RELEASE_DA", "a"."MS" AS "MS", '
347 sqlbioregspatial1 += '"a"."SITETYPE" AS "SITETYPE", "a"."Geometry" AS "Geometry", '
348 sqlbioregspatial1 += '"b"."SITECODE" AS "SITECODE_1", "b"."BIOGEFRAPHICREG" AS "BIOGEFRAPHICREG", '
349 sqlbioregspatial1 += '"b"."PERCENTAGE" AS "PERCENTAGE" '
350 sqlbioregspatial1 += 'FROM "Natura2000polygon" AS "a" '
351 sqlbioregspatial1 += 'JOIN %s AS "b" USING ("SITECODE") ' % (biogeoreg_view)
352 sqlbioregspatial1 += 'ORDER BY "a"."SITECODE";'
353 grass.message ( sqlbioregspatial1 )
354 c.execute( sqlbioregspatial1)
355 # create spatial view of defined biogeographical region - part 2
356 sqlbioregspatial2 = 'INSERT INTO views_geometry_columns '
357 sqlbioregspatial2 += '(view_name, view_geometry, view_rowid, f_table_name, f_geometry_column, read_only) '
358 sqlbioregspatial2 += 'VALUES ("%s", "geometry", "rowid", "natura2000polygon", "geometry", 1);' % (biogeoreg_spatial_view.lower())
359 grass.message ( sqlbioregspatial2 )
360 # execute spatial view
361 c.execute( sqlbioregspatial2 )
362 conn.commit()
363 conn.close()
364 # import spatial view
365 grass.message ( "importing data..." )
366 grass.message ( "may take some time..." )
367 grass.run_command( "v.in.ogr", input = "%s" % (n2k_input),
368 layer = "%s" % (biogeoreg_spatial_view),
369 output = n2k_output,
370 quiet = False)
371
372 if layer_exist :
373 grass.message( "importing existing spatial layer %s of the dataset" % layer_exist )
374 grass.run_command( "v.in.ogr", input = "%s" % (n2k_input),
375 layer = "%s" % (layer_exist),
376 output = n2k_output,
377 quiet = False)
378
379if __name__ == "__main__":
380 options, flags = grass.parser()
381 sys.exit(main())
Note: See TracBrowser for help on using the repository browser.