ecoscape-utilities 0.0.37__tar.gz → 0.0.39__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of ecoscape-utilities might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ecoscape-utilities
3
- Version: 0.0.37
3
+ Version: 0.0.39
4
4
  Summary: A collection of EcoScape utilities.
5
5
  Author-email: Luca de Alfaro <luca@ucsc.edu>, Coen Adler <ctadler@ucsc.edu>, Artie Nazarov <anazarov@ucsc.edu>, Natalia Ocampo-Peñuela <nocampop@ucsc.edu>, Jasmine Tai <cjtai@ucsc.edu>, Natalie Valett <nvalett@ucsc.edu>
6
6
  Project-URL: Homepage, https://github.com/ecoscape-earth/ecoscape-utilities
@@ -11,6 +11,34 @@ from pyproj.crs import CRS
11
11
 
12
12
  from scgt import GeoTiff
13
13
 
14
+ def expand_sqlite_query(query, params):
15
+ """
16
+ Expands an SQLite query string with named parameters from a dictionary.
17
+
18
+ Args:
19
+ query (str): The SQL query string with :variable placeholders.
20
+ params (dict): A dictionary mapping variable names to their values.
21
+
22
+ Returns:
23
+ str: The expanded SQL query string.
24
+ """
25
+ expanded_query = query
26
+ for key, value in params.items():
27
+ placeholder = f":{key}"
28
+ if isinstance(value, str):
29
+ # Escape any single quotes within the string
30
+ formatted_value = f"'{value.replace("'", "''")}'"
31
+ elif value is None:
32
+ formatted_value = 'NULL'
33
+ else:
34
+ # For integers, floats, and other types
35
+ formatted_value = str(value)
36
+
37
+ expanded_query = expanded_query.replace(placeholder, formatted_value)
38
+
39
+ return expanded_query
40
+
41
+
14
42
  """
15
43
  A module for interaction with a sqlite database. Contains functions for query execution,
16
44
  and some common functionality we need to run on the DB
@@ -120,6 +148,7 @@ class EbirdObservations(Connection):
120
148
  query_string = " ".join(query_string)
121
149
  if verbose:
122
150
  print("Query:", query_string)
151
+ print("Expanded query:", expand_sqlite_query(query_string, d))
123
152
  squares_list = self.execute_query((query_string, d))
124
153
  return [sq[0] for sq in squares_list]
125
154
 
@@ -197,10 +226,13 @@ class EbirdObservations(Connection):
197
226
  # Adds breeding portion
198
227
  if breeding is not None:
199
228
  query_string.extend([
200
- 'and substr("OBSERVATION DATE", 6, 2) >= ":br1"',
201
- 'and substr("OBSERVATION DATE", 6, 2) <= ":br2"',
229
+ 'and substr(checklist."OBSERVATION DATE", 6, 2) >= ":br1"',
230
+ 'and substr(checklist."OBSERVATION DATE", 6, 2) <= ":br2"',
202
231
  ])
203
232
  d['br1'], d['br2'] = breeding
233
+ if min_time is not None:
234
+ query_string.append('and "checklist.DURATION MINUTES" >= :min_time')
235
+ d["min_time"] = min_time
204
236
  if date_range is not None:
205
237
  query_string.append('and checklist."OBSERVATION DATE" >= :min_date')
206
238
  query_string.append('and checklist."OBSERVATION DATE" <= :max_date')
@@ -220,6 +252,7 @@ class EbirdObservations(Connection):
220
252
  query_string = " ".join(query_string)
221
253
  if verbose:
222
254
  print("Query:", query_string)
255
+ print("Expanded query:", expand_sqlite_query(query_string, d))
223
256
  r = self.execute_query((query_string, d))
224
257
  if r is None:
225
258
  num_birds = 0
@@ -237,7 +270,7 @@ class EbirdObservations(Connection):
237
270
  )
238
271
 
239
272
  def get_square_individual_checklists(self, square, bird,
240
- breeding=None, date_range=None,
273
+ breeding=None, date_range=None, min_time=None,
241
274
  lat_range=None, lng_range=None, max_dist=2,
242
275
  verbose=False):
243
276
  """
@@ -270,6 +303,9 @@ class EbirdObservations(Connection):
270
303
  'and substr("OBSERVATION DATE", 6, 2) <= ":br2"',
271
304
  ])
272
305
  d['br1'], d['br2'] = breeding
306
+ if min_time is not None:
307
+ query_string.append('and "DURATION MINUTES" >= :min_time')
308
+ d["min_time"] = min_time
273
309
  if date_range is not None:
274
310
  query_string.append('and "OBSERVATION DATE" >= :min_date')
275
311
  query_string.append('and "OBSERVATION DATE" <= :max_date')
@@ -301,14 +337,17 @@ class EbirdObservations(Connection):
301
337
  # Adds breeding portion
302
338
  if breeding is not None:
303
339
  query_string.extend([
304
- 'and substr("OBSERVATION DATE", 6, 2) >= ":br1"',
305
- 'and substr("OBSERVATION DATE", 6, 2) <= ":br2"',
340
+ 'and substr(checklist."OBSERVATION DATE", 6, 2) >= ":br1"',
341
+ 'and substr(checklist."OBSERVATION DATE", 6, 2) <= ":br2"',
306
342
  ])
307
343
  d['br1'], d['br2'] = breeding
308
344
  if date_range is not None:
309
345
  query_string.append('and checklist."OBSERVATION DATE" >= :min_date')
310
346
  query_string.append('and checklist."OBSERVATION DATE" <= :max_date')
311
347
  d["min_date"], d["max_date"] = date_range
348
+ if min_time is not None:
349
+ query_string.append('and checklist."DURATION MINUTES" >= :min_time')
350
+ d["min_time"] = min_time
312
351
  if lat_range is not None:
313
352
  query_string.append('and checklist."LATITUDE" >= :min_lat')
314
353
  query_string.append('and checklist."LATITUDE" <= :max_lat')
@@ -324,6 +363,7 @@ class EbirdObservations(Connection):
324
363
  query_string = " ".join(query_string)
325
364
  if verbose:
326
365
  print("Query:", query_string)
366
+ print("Expanded query:", expand_sqlite_query(query_string, d))
327
367
  rows = self.execute_query((query_string, d))
328
368
  counts = defaultdict(int)
329
369
  for r in rows:
@@ -331,7 +371,7 @@ class EbirdObservations(Connection):
331
371
  checklists_df["Count"] = checklists_df.apply(lambda row : counts[row["SAMPLING EVENT IDENTIFIER"]], axis=1)
332
372
  return checklists_df
333
373
 
334
- def get_squares_with_bird(self, bird, max_dist=1, breeding=None,
374
+ def get_squares_with_bird(self, bird, max_dist=1, breeding=None, min_time=None,
335
375
  date_range=None, lat_range=None, lng_range=None,
336
376
  state=None, verbose=False):
337
377
  """Gets all the squares where a bird has been sighted. This is used
@@ -364,6 +404,9 @@ class EbirdObservations(Connection):
364
404
  'and substr("OBSERVATION DATE", 6, 2) <= ":br2"',
365
405
  ])
366
406
  d['br1'], d['br2'] = breeding
407
+ if min_time is not None:
408
+ query_string.append('and "DURATION MINUTES" >= :min_time')
409
+ d["min_time"] = min_time
367
410
  if date_range is not None:
368
411
  query_string.append('and checklist."OBSERVATION DATE" >= :min_date')
369
412
  query_string.append('and checklist."OBSERVATION DATE" <= :max_date')
@@ -380,6 +423,7 @@ class EbirdObservations(Connection):
380
423
  query_string = " ".join(query_string)
381
424
  if verbose:
382
425
  print("Query:", query_string)
426
+ print("Expanded query:", expand_sqlite_query(query_string, d))
383
427
  r = self.execute_query((query_string, d))
384
428
  return [row[0] for row in r]
385
429
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ecoscape-utilities
3
- Version: 0.0.37
3
+ Version: 0.0.39
4
4
  Summary: A collection of EcoScape utilities.
5
5
  Author-email: Luca de Alfaro <luca@ucsc.edu>, Coen Adler <ctadler@ucsc.edu>, Artie Nazarov <anazarov@ucsc.edu>, Natalia Ocampo-Peñuela <nocampop@ucsc.edu>, Jasmine Tai <cjtai@ucsc.edu>, Natalie Valett <nvalett@ucsc.edu>
6
6
  Project-URL: Homepage, https://github.com/ecoscape-earth/ecoscape-utilities
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "ecoscape-utilities"
7
- version = "0.0.37"
7
+ version = "0.0.39"
8
8
  authors = [
9
9
  {name="Luca de Alfaro", email="luca@ucsc.edu"},
10
10
  {name="Coen Adler", email="ctadler@ucsc.edu"},