Skip to content
Snippets Groups Projects
Commit a5ed3cb7 authored by Julian Stürmer's avatar Julian Stürmer
Browse files

Add functions for plotting a grid onto a map

parent 317e15f8
No related branches found
No related tags found
No related merge requests found
......@@ -7,6 +7,27 @@ Function for recursively merging two dictionaries (used for updating the diction
_recursive_merge(x::AbstractDict,y::AbstractDict) = merge(_recursive_merge,x,y)
_recursive_merge(x,y) = y
#=
Returns the minimal and maximal values of bus longitude and latitude coordinates contained a dictionary. An offset can be used to arbitrarily increase the area.
=#
function _get_pg_area(pos::Dict{Int64,Tuple{Float64,Float64}}, offset=0.)
locs = collect(values(pos))
lon_min = minimum(p -> p[1], locs) - offset
lon_max = maximum(p -> p[1], locs) + offset
lat_min = minimum(p -> p[2], locs) - offset
lat_max = maximum(p -> p[2], locs) + offset
return lon_min, lon_max, lat_min, lat_max
end
function _get_pg_area(network_data::Dict{String,<:Any}, offset=0.)
pos = Dict(
b["index"] => (b["bus_lon"], b["bus_lat"])
for b in collect(values(network_data["bus"]))
) # geographic bus locations
return _get_pg_area(pos, offset)
end
#=
Returns a dictionary of default plot settings for various plotting functions.
=#
......@@ -76,6 +97,59 @@ end
#*------------------------------------------------------------------------------
function plot_pg_map(
network_data::Dict{String,<:Any},
settings = Dict{String,Any}(); # dictionary containing plot settings
figpath::String # where to save the figure
)
### Python imports
cartopy = pyimport("cartopy")
cticker = pyimport("cartopy.mpl.ticker")
### Setup figure and plot geographic map
fig::Figure = plt.figure()
w::Float64, h::Float64 = plt.figaspect(2/3)::Vector{Float64}
fig.set_size_inches(1.5w, 1.5h)
ax = fig.add_subplot(projection=cartopy.crs.PlateCarree())
ax.set_aspect("equal")
### Set area to plot
xmin, xmax, ymin, ymax = _get_pg_area(network_data, 0.4)
ax.set_extent([xmin, xmax, ymin, ymax])
### Get state borders
states_provinces = cartopy.feature.NaturalEarthFeature(
category="cultural",
name="admin_1_states_provinces_lines",
scale="50m",
facecolor="none"
)
### Add wanted features to plot
ax.add_feature(cartopy.feature.LAND)
ax.add_feature(cartopy.feature.OCEAN)
ax.add_feature(cartopy.feature.COASTLINE)
ax.add_feature(cartopy.feature.BORDERS)
ax.add_feature(states_provinces, edgecolor="gray")
### Show longitude and latitude values
gl = ax.gridlines(crs=cartopy.crs.PlateCarree(), draw_labels=true)
gl.xlabels_top = false
gl.ylabels_right = false
gl.xlines = false
gl.ylines = false
gl.xformatter = cartopy.mpl.gridliner.LONGITUDE_FORMATTER
gl.yformatter = cartopy.mpl.gridliner.LATITUDE_FORMATTER
### Set plot settings and plot power grid
settings = _recursive_merge(_default_settings(:plot_pg), settings)
_plot_pg!(ax, network_data, settings, figpath)
return nothing
end
#*------------------------------------------------------------------------------
#=
Plots the power grid described by the network data dictionary (NDD). Possible plot settings are shown in _default_settings.
=#
......@@ -84,13 +158,14 @@ function plot_pg(
settings = Dict{String,Any}(); # dictionary containing plot settings
figpath::String # where to save the figure
)
### Setup figure
figure::Figure, ax::PyObject = plt.subplots()::Tuple{Figure,PyObject}
ax.set_aspect("equal")
w::Float64, h::Float64 = plt.figaspect(2/3)::Vector{Float64}
figure.set_size_inches(1.5w, 1.5h)
ax.set_aspect("equal")
### Set plot settings
### Set plot settings and plot power grid
settings = _recursive_merge(_default_settings(:plot_pg), settings)
_plot_pg!(ax, network_data, settings, figpath)
......@@ -174,13 +249,16 @@ function _draw_buses!(
settings::Dict{String,<:Any} # dictionary containing plot settings
)
### Matplotlib imports
### Python imports
nx = pyimport("networkx")
mlines = pyimport("matplotlib.lines")
bustypes = get_bustypes(network_data) # types of all buses
pos = get_locs(network_data) # geographic bus locations
bus_markes = [
pos = Dict(
b["index"] => (b["bus_lon"], b["bus_lat"])
for b in collect(values(network_data["bus"]))
) # geographic bus locations
bus_markers = [
mlines.Line2D([], [], color=b["color"], marker=b["marker"], ls="None")
for b in collect(values(settings["Buses"]))
if b["label"] != "nolabel"
......@@ -253,10 +331,13 @@ function _draw_br_equal!(
br_settings::Dict{String,<:Any} # dictionary containing plot settings
)
### Matplotlib imports
### Python imports
nx = pyimport("networkx")
pos = get_locs(network_data) # geographic bus locations
pos = Dict(
b["index"] => (b["bus_lon"], b["bus_lat"])
for b in collect(values(network_data["bus"]))
) # geographic bus locations
branches = collect(values(network_data["branch"])) # branch dictionaries
### Get edges contained in the NDD
......@@ -290,10 +371,13 @@ function _draw_br_branchloads!(
br_settings::Dict{String,<:Any} # dictionary containing plot settings
)
### Matplotlib imports
### Python imports
nx = pyimport("networkx")
pos = get_locs(network_data) # geographic bus locations
pos = Dict(
b["index"] => (b["bus_lon"], b["bus_lat"])
for b in collect(values(network_data["bus"]))
) # geographic bus locations
branches = collect(values(network_data["branch"])) # branch dictionaries
br_coloring = br_settings["br_coloring"] # what kind of loading to use
br_status = br_settings["br_status"]
......@@ -342,11 +426,14 @@ function _draw_br_voltage!(
br_settings::Dict{String,<:Any} # dictionary containing plot settings
)
### Matplotlib imports
### Python imports
nx = pyimport("networkx")
mlines = pyimport("matplotlib.lines")
pos = get_locs(network_data) # geographic bus locations
pos = Dict(
b["index"] => (b["bus_lon"], b["bus_lat"])
for b in collect(values(network_data["bus"]))
) # geographic bus locations
branches = collect(values(network_data["branch"])) # branch dictionaries
br_markers = Array{PyObject,1}() # markers for legend
br_labels = Array{String,1}() # labels for legend
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment