Skip to content

Instantly share code, notes, and snippets.

View bohdanszymanik's full-sized avatar

Bohdan Szymanik bohdanszymanik

  • Wellington, New Zealand
View GitHub Profile
@bohdanszymanik
bohdanszymanik / wgtn_cctv_vtp.R
Created August 1, 2025 04:08
Wellington CCTV crime comparison cleaner
library(tidyverse)
library(sf)
library(tictoc)
library(future)
library(units)
#library(remotes)
#install_github("yonghah/esri2sf")
library("esri2sf")
@bohdanszymanik
bohdanszymanik / wgtn_cctv.R
Last active July 31, 2025 02:04
Analysis of the impact of public monitored cctv on wellington crime
library(tidyverse)
library(sf)
# quick aside... testing out sql querying in R - never done this before
# works well
# library(sqldf)
# VARB <- read_csv("C:\\Users\\...\\VictimisationsAgeROVBoundary.csv")
# slice <- sqldf("select * from VARB limit 10")
###################################################################
@bohdanszymanik
bohdanszymanik / example.py
Created June 12, 2025 05:36
Dissolving together Territorial Authority Local Boards TALB to Territorial Authority areas
import arcpy
# Input and output feature classes
in_fc = r"C:\Users\BhodanSzymanilk\wd\SimplifiedVictimisationByStationAtTAGrain\SimplifiedVictimisationByStationAtTAGrain.gdb\census2023TALBpopulations"
output_fc = r"C:\Users\BhodanSzymanilk\wd\SimplifiedVictimisationByStationAtTAGrain\SimplifiedVictimisationByStationAtTAGrain.gdb\Census2023TApopulations"
# create a new field that uniquely identifies each TA - we grab the first 3 numbers from TALB2023_V - check if it exists firts
# all the Auckland local boards are identified with an initial '076' followed by numbers specific to the local board
TAID = "TAID"
for fieldname in [field.name for field in arcpy.ListFields(in_fc)]:
@bohdanszymanik
bohdanszymanik / ScrapingArcGISOnlineMaps.txt
Created June 6, 2025 03:39
Scraping ArcGIS Online maps
When trying to find the locations of CCTV cameras from public maps on arcgis online...
Use this approach https://gis.stackexchange.com/questions/394291/how-to-scrape-extract-data-from-esri-arcgis-from-website
eg for https://wcc.maps.arcgis.com/apps/webappviewer/index.html?id=17900f3db77548c689911c8180de1eb6
you can see
GET https://services1.arcgis.com/CPYspmTk3abe6d7i/arcgis/rest/services/CCTV_City_Safety_Camera_Locations_(View_layer)/FeatureServer/0/query?f=pbf&where=1%3D1&returnGeometry=true&spatialRel=esriSpatialRelIntersects&outFields=*&maxRecordCountFactor=4&outSR=102100&resultOffset=0&resultRecordCount=8000&cacheHint=true&quantizationParameters=%7B%22mode%22%3A%22edit%22%7D
ESRI default is to return protobuf
@bohdanszymanik
bohdanszymanik / choroplethnotes.txt
Created June 5, 2025 23:44
Choropleth map in ArcGIS Pro with range slider
Before I forget...
Starting with NZ Police Station boundaries - clipping to nz polygon coastline, and
using an extract from policedata.nz to get victimisation data.
Victimisation data is year, police station name, sumvictimisation.
To get the one to many join to work for some reason I had to copy the imported csv victimisation data over to the geodatabase and
in the docs it said I also needed to create a unique row id - so I did that first using monotonically_increasing_id() AS vid
@bohdanszymanik
bohdanszymanik / ArcGISProToArcGISOnlineChoroplethMapping.txt
Created June 4, 2025 22:23
Using ArcGIS Pro with ArcGIS Online to generate a choropleth map and save on ArcGIS Online credits
Load in the shape file with the boundaries
Load in the csv file with victimisations or some other measure + boundary key eg meshblock id. Little issue here - by default ArcGIS Pro will strip preceding zeros on a number field and make it an int/long. You can create and apply a function in ArcGIS Pro to convert back to string ids with preceeding zeros or you can use a schema.ini file to enforce a schema on the csv import (that's what I did).
Use the Join tool (not join features tool - because in this instance joining a feature layer to a data table) to join the data to the feature layer containing the boundaries - keyed on eg meshblock id.
Now here's the trick - a simple share to ArcGIS Online will fail because in memory joins aren't supported 00226: In-memory joins are not supported—ArcGIS Pro | Documentation so I did the option in the help doc of exporting out the features to a new feature layer, removing the join and sharing the new feature layer to ArcGIS Online. This worked.
Click the Analyze button first
@bohdanszymanik
bohdanszymanik / fabric_arcgis.py
Created May 27, 2025 00:21
Fabric Notes For ArcGIS Data Prep
# Create an environment to ensure correct python packages are loaded
# Not sure what exactly is needed - I went with the arcgis package
# Create a notebook to hold the python code to filter/aggregate data and push up via the arcgis api
# Create a pipeline to exec the notebook on schedule with parameters eg credentials etc
# Set necessary security / access control on who can see the parameters
# Create a parameters cell in the notebook
pwd="og_value"
# Set up logging to the Lakehouse filesystem so we can track what's happening eg
import logging
@bohdanszymanik
bohdanszymanik / clip_akld.R
Last active August 1, 2025 04:10
Clip meshblock 2013 to Auckland area - copied from a Microsoft Fabric Spark(R) notebook so I st_read file will be an odd location
library(sf)
library(dplyr)
# you need the polygon dataset not the polyline one!
nz_coastline <- st_read("/lakehouse/default/Files/lds-nz-coastlines-and-islands-polygons-topo-150k-SHP/nz-coastlines-and-islands-polygons-topo-150k.shp")
mb2013_akld <- st_read("/lakehouse/default/Files/statsnz-meshblock-2013-SHP-akld/meshblock-2013.shp")
# different CRS for the two regions
# nz_coastline is NZGD2000 while mb2013_akld is WGS 84
# Let's reconcile both to WGS84
@bohdanszymanik
bohdanszymanik / MapVictimisations.ipynb
Created April 27, 2025 00:15
Mapping crime victimisations from the nz police data site
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@bohdanszymanik
bohdanszymanik / example.R
Created November 6, 2023 00:28
From a tibble map a (slow) function over a column to calculate a new column with a progress bar
library(tidyverse)
1:1000 |>
as_tibble_col(column_name="some_column") |>
select(some_column) |>
mutate(some_other_column = map(some_column, function(x){Sys.sleep(1/100);(1/x)}, .progress = list(
type = "iterator",
format = "Calculating {cli::pb_bar} {cli::pb_percent}",
clear = TRUE)))