Skip to content

Instantly share code, notes, and snippets.

View maxcollombin's full-sized avatar

Maxime Collombin maxcollombin

View GitHub Profile
@maxcollombin
maxcollombin / data-gov-csw-howto.rst
Created October 25, 2022 14:23 — forked from kalxas/data-gov-csw-howto.rst
Data.gov CSW HowTo
@maxcollombin
maxcollombin / postgres-cheatsheet.md
Created October 11, 2022 09:55 — forked from Kartones/postgres-cheatsheet.md
PostgreSQL command line cheatsheet

PSQL

Magic words:

psql -U postgres

Some interesting flags (to see all, use -h or --help depending on your psql version):

  • -E: will describe the underlaying queries of the \ commands (cool for learning!)
  • -l: psql will list all databases and then exit (useful if the user you connect with doesn't has a default database, like at AWS RDS)
@maxcollombin
maxcollombin / nearest_neighbour_bigquery.sql
Created August 12, 2022 13:08 — forked from wriglz/nearest_neighbour_bigquery.sql
Spatial SQL to return the id of the closest geometry from table_b to each geometry in table_a
SELECT
a.id,
-- Note that on line 13 the results are grouped by a.id so if you want to return the geometry from table_a,
-- here you can use the function ANY_VALUE(a.geom) (as you cannot group by geometry in BigQuery)
ARRAY_AGG(b.id ORDER BY ST_Distance(a.geog, b.geog) LIMIT 1) [ORDINAL(1)] AS neighbor_id
-- Here we return the id of the closest geometry from table_b to each geometry in table_a.
-- If you want to include more fields from table_b here you can use STRUCT(b.id, b.second_field, b.third_field) etc.
FROM
table_a a
JOIN
@maxcollombin
maxcollombin / snap_points_to_lines.sql
Created July 25, 2022 06:29 — forked from wriglz/snap_points_to_lines.sql
SQL to snap points to the closest line within a predefined radius
-- Snap the points to their closest lines, found in the subquery below
SELECT
point_id,
line_id,
ST_LINE_INTERPOLATE_POINT(line_geom,
ST_Line_Locate_Point(line_geom, point_geom)) AS snapped_points --Create the snapped points
FROM
--Subquery to find the closest line to each point (within a pre-defined raidus)
(