Skip to content

Instantly share code, notes, and snippets.

View daliposc's full-sized avatar
🌎

Connor Daliposon daliposc

🌎
  • Portland, OR
View GitHub Profile
@daliposc
daliposc / line-segment-bearing-and-distance.py
Last active December 6, 2019 21:22
Calculates the bearing and distance for each segment of a Linestring and stores the data in a Points layer with a feature for each vertex of the original line.
from math import pi
line_lyr = QgsProject.instance().mapLayersByName('line')[0]
line_geom = line_lyr.getGeometry(1)
results_lyr = QgsVectorLayer(f'Point?crs={line_lyr.sourceCrs().authid()}', 'temp', 'memory')
results_provider = results_lyr.dataProvider()
results_provider.addAttributes([QgsField('id', QVariant.Int),
QgsField('dist_along_line', QVariant.Double),
QgsField('dist_from_previous_point', QVariant.Double),
@daliposc
daliposc / draw_shortest_path_from_origin_to_destination.py
Created December 3, 2019 09:09
Some person on reddit asked how they could use QGIS to calculate the number of police stations within 5 miles of a school. I made them a small tutorial.
from collections import defaultdict
# Initialize layer variables
streets_lyr = QgsProject.instance().mapLayersByName('streets')[0]
origin_lyr = QgsProject.instance().mapLayersByName('schools')[0]
destination_lyr = QgsProject.instance().mapLayersByName('police_stations')[0]
od_matrix = QgsProject.instance().mapLayersByName('od_pairs')[0]
# Initialize variables for analysis
origin_ids = QgsExpression('array_agg("fid")').evaluate(origin_lyr.createExpressionContext())
@daliposc
daliposc / gtfs_join_routes_to_stops_shape_geom.sql
Last active December 2, 2019 10:51
Create PostGIS geometry tables from GTFS stops and shapes. Then join geom tables with route info. Tested with Corvallis GTFS data, which is the reason for reprojecting from EPSG:4326 to EPSG:2913. Also, SQL is weird.
create table route_lines as (
select r.*, s.geom
from (
select route_id, route_short_name, route_long_name, route_color, min_headway_minutes
from routes
group by route_id
) r
join (
select route_id, shape_id
from trips
@daliposc
daliposc / ogr2ogr_cookbook.md
Last active December 3, 2019 09:11
ogr2ogr cookbook. a very small cookbook (so far).

Batch command, all GeoJSON in folder —> PostGIS

FOR %%f IN (*.geojson) DO (
	ogr2ogr -f PostgreSQL PG:"dbname={dbname} user={user} password={pass}" %%f
)

Batch command, Numbered files —> PostGIS

Run as: to_postgis.bat [base_filename] [file_extenstion] [num_files]. Assumes first file is "[base_filename]_0.[file_extension]".