Skip to content

Commit

Permalink
Merge pull request #5 from NREL-Sienna/bugfix
Browse files Browse the repository at this point in the history
Bugfix
  • Loading branch information
claytonpbarrows authored Sep 28, 2024
2 parents f43a88f + 8d0aa78 commit f8b9cac
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 220 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "PowerSystemsMaps"
uuid = "e146f72c-d4f8-45d3-b3ca-12a16cb68a38"
authors = ["cbarrows <clayton.barrows@nrel.gov>"]
version = "0.2.0"
version = "0.2.1"

[deps]
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
Expand Down
2 changes: 1 addition & 1 deletion src/PowerSystemsMaps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export plot_net
export plot_net!
export plot_components!
export make_graph
export plot_map

include("plot_network.jl")
include("sfdp_fixed.jl")

end # module PowerSystemsMaps
92 changes: 70 additions & 22 deletions src/plot_network.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,10 @@ function make_graph(sys::PowerSystems.System; kwargs...)
:name => get_name(b),
:number => get_number(b),
:area => get_name(get_area(b)),
:fixed => false,
)
if has_supplemental_attributes(GeographicInfo, b)
(lon, lat) = get_lonlat(b)
data[:initial_position] = PT(lon, lat)
data[:fixed] = true
end
g[get_name(b)] = data
end
Expand All @@ -105,24 +103,22 @@ function make_graph(sys::PowerSystems.System; kwargs...)
# layout nodes
a = adjacency_matrix(g) # generates a sparse adjacency matrix

fixed = get_prop(g, :fixed)
sort_ids = vcat(findall(fixed), findall(.!fixed))
orig_ids = sortperm(sort_ids)
sorted_a = a[sort_ids, sort_ids]

@info "calculating node locations with SFDP_Fixed"
K = get(kwargs, :K, 0.1)
ip = get_prop(g, :initial_position)
setdiff!(ip, ip[findall(isnothing, ip)])
network = sfdp_fixed(
sorted_a;
tol = 1.0,
C = 0.0002,
K = K,
iterations = 100,
fixed = true,
initialpos = ip,
)[orig_ids] # generate 2D layout and sort back to order of a
ip = Dict(zip(1:nv(g), get_prop(g, :initial_position)))
filter!(p -> !isnothing(last(p)), ip)

if length(ip) != nv(g)
@info "calculating node locations with SFDP"
network = NetworkLayout.sfdp(
a;
tol = get(kwargs, :tol, 1.0),
C = get(kwargs, :C, 0.0002),
K = get(kwargs, :K, 0.1),
iterations = get(kwargs, :iterations, 100),
pin = ip,
)
else
network = ip
end

set_prop!(g, :x, first.(network))
set_prop!(g, :y, last.(network))
Expand Down Expand Up @@ -256,12 +252,64 @@ function plot_net!(p::Plots.Plot, g; kwargs...)
return p
end

"""
map network on top of basemap
Accepted kwargs for network plot:
- `lines::Bool` : show lines
- `linealpha::Union{Float, Vector{Float}}` : line transparency
- `nodesize::Union{Float, Vector{Float}}` : node size
- `nodehover::Union{String, Vector{String}}` : node hover text
- `linewidth::Float` : width of lines
- `linecolor::String` : line color
- `buffer::Float`
- `size::Tuple` : figure size
- `xlim::Tuple` : crop x axis
- `ylim::Tuple` : crop y axis
- `showleged::Bool` : show legend
- `nodecolor::String` : node color
- `nodealpha::Float` : node transparency
- `legend_font_color::String` : legend font color
Accepted kwargs for map:
- `map_lines::Bool` : show lines
- `map_linealpha::Union{Float, Vector{Float}}` : line transparency
- `map_nodesize::Union{Float, Vector{Float}}` : node size
- `map_nodehover::Union{String, Vector{String}}` : node hover text
- `map_linewidth::Float` : width of lines
- `map_linecolor::String` : line color
- `map_buffer::Float`
- `map_size::Tuple` : figure size
- `map_xlim::Tuple` : crop x axis
- `map_ylim::Tuple` : crop y axis
- `map_showleged::Bool` : show legend
- `map_nodecolor::String` : node color
- `map_nodealpha::Float` : node transparency
- `map_legend_font_color::String` : legend font color
"""
function plot_map(sys::System, shapefile::AbstractString; kwargs...)
g = make_graph(sys; kwargs...)
shp = Shapefile.shapes(Shapefile.Table(shapefile))
shp = lonlat_to_webmercator(shp) #adjust coordinates

map_kwargs = filter(x -> startswith(string(first(x)), "map_"), kwargs)
kwargs = setdiff(kwargs, map_kwargs)
map_kwargs = [Symbol(replace(string(k), "map_" => "")) => v for (k, v) in map_kwargs]

# plot a map from shapefile
p = plot(shp; map_kwargs...)

# plot the network on the map
p = plot_net!(p, g; kwargs...)
return p
end

# borrowed from
function lonlat_to_webmercator(xLon, yLat)

# Check coordinates are in range
abs(xLon) <= 180 || throw("Maximum longitude is 180.")
abs(yLat) < 85.051129 || throw(
abs(xLon) > 180 && throw("Maximum longitude is 180.")
abs(yLat) >= 85.051129 && throw(
"Web Mercator maximum lattitude is 85.051129. This is the lattitude at which the full map becomes a square.",
)

Expand Down
188 changes: 0 additions & 188 deletions src/sfdp_fixed.jl

This file was deleted.

1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
PlotlyJS = "f0f68f2c-4968-5e81-91da-67840de0976a"
PowerSystems = "bcd98974-b02a-5e2f-9ee0-a103f5c450dd"
PowerSystemsMaps = "e146f72c-d4f8-45d3-b3ca-12a16cb68a38"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
8 changes: 5 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ LOG_LEVELS = Dict(

function make_test_sys()
sys = System(joinpath(TEST_DIR, "test_data", "case_ACTIVSg200.m"))
locs = CSV.read(joinpath(TEST_DIR, "test_data", "bus_locs.csv"), DataFrames.DataFrame)
for row in DataFrames.eachrow(locs)
locs = PSY.CSV.read(
joinpath(TEST_DIR, "test_data", "bus_locs.csv"),
PSY.DataFrames.DataFrame,
)
for row in PSY.DataFrames.eachrow(locs)
bus = first(get_components(x -> occursin(row.name, get_name(x)), Bus, sys))
if !isnothing(bus)
#set_ext!(bus, Dict("latitude" => row.latitude, "longitude" => row.longitude))
add_supplemental_attribute!(
sys,
bus,
Expand Down
28 changes: 23 additions & 5 deletions test/test_maps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ sys = make_test_sys()
g = make_graph(sys; K = 0.01)
@test typeof(g) <: PSM.MetaGraphsNext.MetaGraph
@test length(PSM.get_prop(g, :x)) == 200
shp = PSM.Shapefile.shapes(
PSM.Shapefile.Table(
joinpath(TEST_DIR, "test_data", "IL_BNDY_County", "IL_BNDY_County_Py.shp"),
),
)
shapefile = joinpath(TEST_DIR, "test_data", "IL_BNDY_County", "IL_BNDY_County_Py.shp")
shp = PSM.Shapefile.shapes(PSM.Shapefile.Table(shapefile))
shp = PSM.lonlat_to_webmercator(shp) #adjust coordinates
@test length(shp) == 102

Expand Down Expand Up @@ -39,4 +36,25 @@ sys = make_test_sys()
buffer = 0.4e4,
)
@test typeof(p) == PSM.Plots.Plot{PSM.Plots.GRBackend}

p = plot_map(
sys,
joinpath(TEST_DIR, "test_data", "IL_BNDY_County", "IL_BNDY_County_Py.shp");
map_fillcolor = "grey",
map_background_color = "white",
map_linecolor = "darkgrey",
map_axis = nothing,
map_border = :none,
map_label = "",
map_legend_font_color = :red,
nodesize = 3.0,
linecolor = "blue",
linewidth = 0.6,
lines = true,
nodealpha = 1.0,
shownodelegend = true,
size = (1500, 800),
buffer = 0.4e4,
)
@test typeof(p) == PSM.Plots.Plot{PSM.Plots.GRBackend}
end

2 comments on commit f8b9cac

@claytonpbarrows
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/116229

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.1 -m "<description of version>" f8b9caceebfdbdbbf8ccaf48a6dc3f3ca97669c9
git push origin v0.2.1

Please sign in to comment.