3030logging .basicConfig ()
3131logger .setLevel (logging .DEBUG )
3232
33+ # Note that this query returns more species that would be accepted into STAR based on
34+ # threat category, but for model checking we want all AOHs for a given taxa, so the
35+ # pipeline must process them all, and then just include the correct set when processing
36+ # STAR
3337MAIN_STATEMENT = """
3438SELECT
3539 assessments.sis_taxon_id as id_no,
5458 AND taxons.class_name = %s
5559 AND taxons.infra_type is NULL -- no subspecies
5660 AND taxons.metadata->>'taxon_level' = 'Species'
57- AND red_list_category_lookup.code IN ('NT', 'VU', 'EN', 'CR')
61+ AND red_list_category_lookup.code IN ('DD', 'LC', ' NT', 'VU', 'EN', 'CR')
5862"""
5963
6064SYSTEMS_STATEMENT = """
@@ -138,20 +142,10 @@ def process_row(
138142 presence += (4 ,)
139143 report .possibly_extinct = True # pylint: disable=W0201
140144
141- cursor .execute (SYSTEMS_STATEMENT , (assessment_id ,))
142- systems_data = cursor .fetchall ()
143- try :
144- systems = process_systems (systems_data , report )
145- except ValueError as exc :
146- logger .debug ("Dropping %s: %s" , id_no , str (exc ))
147- return report
148-
149- cursor .execute (THREATS_STATEMENT , (assessment_id ,))
150- raw_threats = cursor .fetchall ()
151- threats = process_threats (raw_threats , report )
152- if len (threats ) == 0 :
153- return report
145+ include_in_star = category in ('NT' , 'VU' , 'EN' , 'CR' )
146+ report .has_category = include_in_star
154147
148+ # First checks are the ones that rule out being able to make an AOH at all
155149 cursor .execute (HABITATS_STATEMENT , (assessment_id ,))
156150 raw_habitats = cursor .fetchall ()
157151 try :
@@ -170,6 +164,37 @@ def process_row(
170164 except ValueError as _exc :
171165 return report
172166
167+ # Second checks are whether it is good for STAR, so from hereon we should
168+ # output a GeoJSON regardless as we should make an AOH for validation with this
169+ cursor .execute (SYSTEMS_STATEMENT , (assessment_id ,))
170+ systems_data = cursor .fetchall ()
171+ try :
172+ systems = process_systems (systems_data , report )
173+ except ValueError as exc :
174+ logger .debug ("Dropping %s: %s" , id_no , str (exc ))
175+ include_in_star = False
176+ try :
177+ systems = systems_data [0 ][0 ]
178+ except IndexError :
179+ systems = []
180+
181+ cursor .execute (THREATS_STATEMENT , (assessment_id ,))
182+ raw_threats = cursor .fetchall ()
183+ threats = process_threats (raw_threats , report )
184+ if len (threats ) == 0 :
185+ include_in_star = False
186+
187+ report .in_star = include_in_star
188+
189+ try :
190+ category_weight = CATEGORY_WEIGHTS [category ]
191+ except KeyError :
192+ assert include_in_star is False
193+ category_weight = 0
194+
195+ # This is a fix as per the method to include the missing islands layer:
196+ habitats_list = list (habitats ) + ["islands" ]
197+
173198 gdf = gpd .GeoDataFrame (
174199 [[
175200 id_no ,
@@ -179,14 +204,15 @@ def process_row(
179204 systems ,
180205 int (elevation_lower ) if elevation_lower is not None else None ,
181206 int (elevation_upper ) if elevation_upper is not None else None ,
182- '|' .join (list ( habitats ) ),
207+ '|' .join (habitats_list ),
183208 scientific_name ,
184209 family_name ,
185210 class_name ,
186211 json .dumps (threats ),
187212 category ,
188- CATEGORY_WEIGHTS [category ],
189- geometry
213+ category_weight ,
214+ geometry ,
215+ include_in_star ,
190216 ]],
191217 columns = COLUMNS ,
192218 crs = target_projection or 'epsg:4326'
0 commit comments