Skip to content

Instantly share code, notes, and snippets.

@Banashek
Created July 21, 2022 19:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Banashek/1cc3d8435843bcff230906fb037eeefd to your computer and use it in GitHub Desktop.
Save Banashek/1cc3d8435843bcff230906fb037eeefd to your computer and use it in GitHub Desktop.
Quick and dirty fable-py prettymaps. Very dynamic for proof of concept.
open Fable.Core
open Fable.Core.PyInterop
open Fable.Python.Json
[<ImportAll("sys")>]
let sys: obj = nativeOnly
let prettymaps: obj = importAll "prettymaps"
let matplotlib: obj = importAll "matplotlib"
let address = "Praça Ferreira do Amaral, Macau"
let radius = 1100
let perimeterSettings = {| |}
let streetLayerSettings =
{| width =
{| motorway = 5
trunk = 5
primary = 4.5
secondary = 4
tertiary = 3.5
residential = 3
service = 2
unclassified = 2
pedestrian = 2
footway = 1 |} |}
let buildingLayerSettings =
{| tags =
{| building = true
landuse = "construction"
union = false |} |}
let waterLayerSettings = {| tags = {| natural = [| "water"; "bay" |] |} |}
let greenLayerSettings =
{| tags =
{| landuse = "grass"
natural = [| "island"; "wood" |]
leisure = "park" |} |}
let forestLayerSettings = {| tags = {| landuse = "forest" |} |}
type ParkingLayerTags =
{ amenity: string
highway: string
[<CompiledName("man_made")>]
manmade: string }
let parkingLayerTags: ParkingLayerTags =
{ amenity = "parking"
highway = "pedestrian"
manmade = "pier" }
let parkingLayerSettings =
{| tags =
{| amenity = "parking"
highway = "pedestrian"
man_made = "pier" |} |}
let layers =
{| perimeter = perimeterSettings
streets = streetLayerSettings
building = buildingLayerSettings
water = waterLayerSettings
green = greenLayerSettings
parking = parkingLayerSettings |}
let drawingKwargs =
{| background =
{| fc = "#f2f4cb"
ec = "#dadbc1"
hatch = "ooo..."
zorder = -1 |}
perimeter =
{| fc = "#f2f4cb"
ec = "#dadbc1"
lw = 0
hatch = "ooo..."
zorder = 0 |}
green =
{| fc = "#d0f1bf"
ec = "#2f3737"
lw = 1
zorder = 1 |}
forest =
{| fc = "#64b96a"
ec = "#2f3737"
lw = 1
zorder = 1 |}
water = {|
fc = "#a1e3ff"
ec = "#2f3737"
hatch = "ooo..."
hatch_c = "#85c9e6"
lw = 1
zorder = 2 |}
parking =
{| fc = "#f2f4cb"
ec = "#2f3737"
lw = 1
zorder = 3 |}
streets =
{| fc = "#2f3737"
ec = "#475657"
alpha = 1
lw = 0
zorder = 3 |}
building =
{| palette = [| "#ffc857"; "#e9724c"; "#c5283d" |]
ec = "#2f3737"
lw = 0.5
zorder = 4 |} |}
let figSize = (12, 12)
[<Emit("matplotlib.pyplot.subplots(figsize = ($0, $1), constrained_layout = $2)")>]
let subplots (_figX: int) (_figY: int) (_constrainedLayout: bool) : obj * obj = nativeOnly
[<Emit("matplotlib.pyplot.savefig($0)")>]
let saveFigure (_path: string) = nativeOnly
[<Emit("prettymaps.plot($0, radius = $1, ax = $2, layers = $3, drawing_kwargs = $4)")>]
let plot (_address: string) (_radius: int) (_axis: obj) (_layers: obj) (_drawing_kwargs: obj) : obj = nativeOnly
/// Main
///
let (fig, ax) = subplots 12 12 true
let plottedLayers = plot address radius ax layers drawingKwargs
saveFigure "./figure.png"
@Banashek
Copy link
Author

Usage:

  1. Create the fable-py project:
    dotnet new console -lang f\#
    dotnet new tool-manifest
    dotnet tool install fable-py --version 4.0.0-alpha-032
    dotnet add package Fable.Core.Experimental --version 4.0.0-alpha-032
    dotnet add package Fable.Python
  2. Swap the program.fs with the gist above
  3. Create the python environment and install the appropriate packages:
    python -m venv .
    chmod +x ./bin/activate
    ./bin/activate
    python -m pip install prettymaps
    python -m pip install osmnx==1.2.1 (See this issue)
  4. Compile and run:
    dotnet fable-py fablepymap.fsproj
    python program.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment