Skip to content

Instantly share code, notes, and snippets.

SELECT
polygons.*,
COUNT(points.*)
FROM polygons, points
WHERE ST_Intersects(points.the_geom, polygons.the_geom)
import pandas as pd
from geopandas import GeoDataFrame, points_from_xy
import geopandas as gpd
import time
# Load the Uber point data
df = pd.read_csv('uber_all.csv')
# Create the GeoDataFrame and assign the proper projection
@mbforr
mbforr / spatial_sql.sql
Last active January 8, 2023 11:31
Example Spatial SQL Post
-- written for PostGIS
-- table states is US State Polygons
-- table capitals are US State Capital points
select
a.name,
a.id,
-- create a centroid of the state polygon
WITH
buildings AS (
SELECT
id,
latitude,
longitude,
username,
osm_timestamp,
FROM
`bigquery-public-data.geo_openstreetmap.planet_nodes`
@mbforr
mbforr / select.sql
Last active January 8, 2023 11:30
Spatial SQL - select
select
*
from
`bigquery-public-data.geo_us_boundaries.states`
@mbforr
mbforr / select_columns.sql
Last active January 8, 2023 11:29
Spatial SQL - select columns
select
state_geom, state_name, state
from
`bigquery-public-data.geo_us_boundaries.states`
@mbforr
mbforr / select_all.sql
Created July 3, 2021 02:41
Spatial SQL - Basic Query
select
*
from bigquery-public-data.geo_us_boundaries.state
select
state_geom, state_name, state
from
`bigquery-public-data.geo_us_boundaries.states`
limit 10
select
state_geom, state_name, state
from
`bigquery-public-data.geo_us_boundaries.states`
limit 10 offset 10
select
state_geom, state_name, state
from
`bigquery-public-data.geo_us_boundaries.states`
# this is a comment - it won't impact your code and you can delete it if you want
# use the where clause to select rows based on a condition
# in this case an exact match to where the state_name column equals Wisconsin