Transfer from flash drive
View presentation on web:
2023-02-22
Transfer from flash drive
View presentation on web:
Aaron Fisch:
Water quality modeler with the Dept. of Natural Resources. Interested in computer programming for automation in modeling/statistics.
Dave Evans:
Soil scientist with a background in GIS, data science and data engineering. He does some consulting work with soils and web development.
Matt Haffner:
Assistant professor at the University of Wisconsin–Eau Claire. He is interested in spatial data science, geocomputation, and urban analytics.
| Time | Length | Content | Speaker |
|---|---|---|---|
| 9:30-9:50 | 20 mins | Intro and Setup (We are here) | Aaron |
| 9:30-10:10 | 20 mins | What is SQLite? | Dave |
| 10:10-10:30 | 20 mins | Databases 101 | Matt |
| 10:30-10:50 | 20 mins | Interactive: Basic SQL | Everyone |
| 10:50-11:00 | 10 mins | Break | NA |
| 11:00-11:20 | 20 mins | Database Views, Why and How | Aaron |
| 11:20-11:40 | 20 mins | Interactive: Spatial Views | Everyone |
| 11:40-12:00 | 20 mins | SQL Geoprocessing | Dave |
| 12:00-12:20 | 20 mins | Interactive: SQL Geoprocessing | Everyone |
| 12:20-12:30 | 10 mins | Wrap-up | Aaron |
./wlia-sqlite-workshop/
Windows users:
QGIS users:
| Format | Size | Viewing speed | Geoproc time |
|---|---|---|---|
| geoJSON | 180 MB | Very poor | 28 sec |
| Shapefile | 60 MB | Good | 23 sec |
| Geopackage | 138 MB | Good | 20 sec |
ESRI Mobile Geodatabase and SpatiaLite
Any questions?
Table A
| State | Governor |
|---|---|
| Wisconsin | Tony Evers |
| California | Gavin Newsom |
| Michigan | Gretchen Whitmer |
| Illinois | J. B. Pritzker |
Table A
| State | Governor |
|---|---|
| Wisconsin | Tony Evers |
| California | Gavin Newsom |
| Michigan | Gretchen Whitmer |
| Illinois | J. B. Pritzker |
Table B
| State | County |
|---|---|
| Wisconsin | Dane |
| Wisconsin | Sauk |
| Wisconsin | Milwaukee |
| Wisconsin | Orange |
A table can function like a feature class with the addition of a geometry column.
Feature Class
| Geometry | State | County |
|---|---|---|
| POLYGON ((-89… | Wisconsin | Dane |
| POLYGON ((-89… | Wisconsin | Sauk |
| POLYGON ((-87… | Wisconsin | Milwaukee |
| POLYGON ((-11… | California | Orange |
Windows users:
QGIS users:
select statementSyntax:
select * from countries
Anatomy of a select statement:
| Command | Which columns | Which table | Table name |
|---|---|---|---|
| select | * | from | countries |
select specific columnsWe can choose to limit our selection to only certain columns
select name, pop_est from countries
| NAME | POP_EST |
|---|---|
| Zimbabwe | 14645468 |
| Zambia | 17861030 |
| Yemen | 29161922 |
| Vietnam | 96462106 |
| Venezuela | 28515829 |
| Vatican | 825 |
| Vanuatu | 299882 |
| Uzbekistan | 33580650 |
| Uruguay | 3461734 |
| Micronesia | 113815 |
select where clause (i.e., specific rows)We can filter rows by using a where clause
select name from countries where continent = 'South America'
| NAME |
|---|
| Venezuela |
| Uruguay |
| Falkland Is. |
| Suriname |
| Peru |
| Paraguay |
| Guyana |
| Ecuador |
| Colombia |
| Chile |
select where clause (i.e., specific rows)select basic mathWe can also do math by column:
select name, pop_est * 1e-6 as pop_est_mil from countries
| NAME | pop_est_mil |
|---|---|
| Zimbabwe | 14.645468 |
| Zambia | 17.861030 |
| Yemen | 29.161922 |
| Vietnam | 96.462106 |
| Venezuela | 28.515829 |
| Vatican | 0.000825 |
| Vanuatu | 0.299882 |
| Uzbekistan | 33.580650 |
| Uruguay | 3.461734 |
| Micronesia | 0.113815 |
select math within groupsGrouping (AKA aggregation) allows us to do math across categories, much like how the Dissolve tools works in Arc.
select continent, sum(pop_est) as total_pop_continent from countries group by continent
| CONTINENT | total_pop_continent |
|---|---|
| Africa | 1307986092 |
| Antarctica | 4490 |
| Asia | 4565778126 |
| Europe | 746437653 |
| North America | 585351846 |
| Oceania | 42430881 |
| Seven seas (open ocean) | 1901993 |
| South America | 427066661 |
select sortingAnd we can sort columns numerically or alphabetically:
select name from countries order by pop_est desc
| NAME |
|---|
| China |
| India |
| United States of America |
| Indonesia |
| Pakistan |
| Brazil |
| Nigeria |
| Bangladesh |
| Russia |
| Mexico |
select with a joinWe can join tables using the SQL join command:
select name, head_of_state, pop_est from countries join south_american_heads_of_state using (name)
| NAME | head_of_state | POP_EST |
|---|---|---|
| Venezuela | Nicolás Maduro | 28515829 |
| Uruguay | Luis Lacalle Pou | 3461734 |
| Suriname | Chan Santokhi | 581363 |
| Peru | Dina Boluarte | 32510453 |
| Paraguay | Mario Abdo Benítez | 7044636 |
| Panama | Laurentino Cortizo | 4246439 |
| Guyana | Irfaan Ali | 782766 |
| Ecuador | Guillermo Lasso | 17373662 |
| Colombia | Gustavo Petro | 50339443 |
| Chile | Gabriel Boric | 18952038 |
Join by an ID when possible, not by a column containing long character strings. Values must match exactly in order for the join to work successfully.
select name, head_of_state, pop_est from countries join south_american_heads_of_state using (name)
select, table references and aliasesWhen we join tables, sometimes SQL gets confused if there are multiple tables with the same column name. We can fix this with either table references or aliases and the . (dot) operator.
select a.name, b.head_of_state, a.pop_est from countries as a join south_american_heads_of_state as b using (name)
| NAME | head_of_state | POP_EST |
|---|---|---|
| Venezuela | Nicolás Maduro | 28515829 |
| Uruguay | Luis Lacalle Pou | 3461734 |
| Suriname | Chan Santokhi | 581363 |
| Peru | Dina Boluarte | 32510453 |
| Paraguay | Mario Abdo Benítez | 7044636 |
| Panama | Laurentino Cortizo | 4246439 |
| Guyana | Irfaan Ali | 782766 |
| Ecuador | Guillermo Lasso | 17373662 |
| Colombia | Gustavo Petro | 50339443 |
| Chile | Gabriel Boric | 18952038 |
Using SpatiaLite or QGIS
Level 1:
How many people live in Australia?
Level 2:
How many millions of citizens live in Gabriel Boric’s country?
Simple: a view is a saved query.
Working with the wi_counties feature class we can extract the area of each Wisconsin county using the spatial st_area function. We can save this view using the create view view_name as syntax.
create view wi_county_area as select fips, st_area(shape) * 3.86102e-7 as area_sq_miles from wi_counties
Let’s convert total population to density by using the view we just created.
create view wi_pop_dens as select fips, round(pop_est / area_sq_miles) as pop_per_sq_mile from wi_counties_pop join wi_county_area using (fips)
What if we wanted to make a view that we can use in a map? We can create a spatial view that is analogous to using the join tool in ArcPro.
create view wi_pop_dens_sv as select objectid, shape, fips, name, pop_per_sq_mile from wi_counties join wi_pop_dens using (fips)
When tables used in views are updated, the views are dynamically updated at the same time
Using SpatiaLite or QGIS
Level 1:
Create a new view of county populations expressed as thousands of people.
Level 2:
Make a point spatial view of county centroids including the population field (you may use multiple views), and make a map that labels those centroids with county population.
ST_Intersects(a.geometry, b.geometry) and ST_Within(a.geometry, b.geometry)ST_Buffer(geometry, distance) and ST_Centroid(geometry)ST_Union(a.geometry, b.geometry) and ST_Intersection(a.geometry, b.geometry)ST_Area(geometry) and ST_Length(geometry)select name, contaminated, st_within(fake_well_data.geom, wi_counties.shape) from wi_counties join fake_well_data on st_within(fake_well_data.geom, wi_counties.shape)
Buffer each well point 5 km (5000 m)
select fid, st_buffer(geom, 5000) as geom from fake_well_data where contaminated=1
select fid, st_union(st_buffer(geom, 5000)) as geom from fake_well_data where contaminated=1
Scenario: We want to resample all uncontaminated wells which are with 5 km of a contaminated well.
Find the wells that need resampling.
select uncont.*
from (
select fid, st_union(st_buffer(geom, 5000)) as geom
from fake_well_data
where contaminated=1
) as cont_ubuff
join (
select *
from fake_well_data
where contaminated = 0
) uncont
on st_within(uncont.geom, cont_ubuff.geom)
group by here)Level 1:
How many people live in Australia?
select pop_est from countries where name = 'Australia'
Level 2:
How many millions of citizens live in Gabriel Boric’s country?
select pop_est * 1e-6 as pop_est_mil from countries join south_american_heads_of_state using (name) where head_of_state = 'Gabriel Boric'
Level 1:
Create a new view of county populations expressed as thousands of people.
create view wi_counties_pop_thousands as select name, pop_est * 1e-3 as pop_est_thousands from wi_counties_pop
Level 2:
Make a point spatial view of county centroids including the population field (you may use multiple views), and make a map that labels those centroids with county population. It may require the use of the SpatiaLite GUI, QGIS DB Manager, or the ArcPro Create Database View tool to register the view properly, in which case line 1 can be omitted from the SQL code below:
create view wi_county_centroids_pop as select a.objectid, st_centroid(shape) as shape, a.name, pop_est from wi_counties as a join wi_counties_pop as b using (fips)
select rivers.name, countries.name, st_intersects(rivers.shape, countries.shape) from rivers join countries on st_intersects(rivers.shape, countries.shape) where countries.continent = "South America"
select max(objectid) as objectid, CONTINENT, st_union(shape) as shape from countries Group by CONTINENT