Skip to content

Commit

Permalink
Adiciona API para plotar ocorrências
Browse files Browse the repository at this point in the history
  • Loading branch information
Phenome committed Feb 8, 2024
1 parent 54d3d6a commit 59a0cc2
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
11 changes: 11 additions & 0 deletions web/src/lib/mongo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ export async function listTaxa(
.toArray()
}

export async function listOccurrences(
filter: Record<string, unknown> = {},
_projection: Record<string, unknown> = {}
) {
const occurrences = await getCollection('dwc2json', 'ocorrencias')
return await occurrences
.find(filter)
// .project(projection)
.toArray()
}

export async function listTaxaPaginated(
filter: Record<string, unknown> = {},
page = 0,
Expand Down
71 changes: 71 additions & 0 deletions web/src/lib/queries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { listOccurrences } from './mongo.ts'

type OccurrenceQuery = {
canonicalName?: string
country?: string
stateProvince?: string
county?: string
recordedBy?: string
eventDate?: string
catalogNumber?: string
lat?: number
long?: number
maxDistance?: number
}
export const getOccurrencePlot = async (query: OccurrenceQuery) => {
const { canonicalName, lat, long, maxDistance } = query
const filter: Record<string, unknown> = {}
if (canonicalName) filter.canonicalName = new RegExp(canonicalName, 'i')
if (lat && long) {
filter.geoPoint = {
$near: {
$geometry: {
type: 'Point',
coordinates: [long, lat]
},
$maxDistance: maxDistance ?? 1000
}
}
} else if (!canonicalName) {
return null
}

type Occurrence = {
[key: string]: unknown
geoPoint?: {
type: 'Point'
coordinates: [number, number]
}
}
const data = (await listOccurrences(filter)) as Occurrence[]
return data.reduce<{
type: 'FeatureCollection'
features: Record<string, unknown>[]
}>(
(acc, cur) => {
if (cur.geoPoint) {
acc.features.push({
type: 'Feature',
properties: {
scientificName: cur.scientificName,
locality: cur.locality,
country: cur.country,
stateProvince: cur.stateProvince,
county: cur.county,
recordedBy: cur.recordedBy,
eventDate: cur.eventDate,
catalogNumber: cur.catalogNumber,
decimalLatitude: cur.decimalLatitude,
decimalLongitude: cur.decimalLongitude
},
geometry: cur.geoPoint
})
}
return acc
},
{
type: 'FeatureCollection',
features: []
}
)
}
32 changes: 32 additions & 0 deletions web/src/pages/api/plotOccurrences.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { APIContext } from 'astro'

import { getOccurrencePlot } from '../../lib/queries.ts'

export async function GET({ request: { url } }: APIContext) {
const searchParams = new URL(url).searchParams
const [canonicalName, lat, long, maxDistance] = [
searchParams.get('canonicalName') ?? '',
searchParams.get('lat'),
searchParams.get('long'),
searchParams.get('maxDistance')
]
const data = await getOccurrencePlot({
canonicalName,
...(lat && long ? { lat: +lat, long: +long } : {}),
...(maxDistance ? { maxDistance: +maxDistance } : {})
})
if (!data) {
return new Response(
'Either canonicalName or Latitude and longitude are required',
{
status: 400
}
)
}

return new Response(JSON.stringify(data), {
headers: {
'Content-Type': 'application/json'
}
})
}

0 comments on commit 59a0cc2

Please sign in to comment.