mirror of
https://github.com/ANL-CEEESA/RELOG.git
synced 2025-12-05 23:38:52 -06:00
GeoDB: Add 2018-us-zcta and us-state
This commit is contained in:
@@ -182,7 +182,6 @@ The keys in the `capacities (tonne)` dictionary should be the amounts (in tonnes
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### Current limitations
|
### Current limitations
|
||||||
|
|
||||||
* Each plant can only be opened exactly once. After open, the plant remains open until the end of the simulation.
|
* Each plant can only be opened exactly once. After open, the plant remains open until the end of the simulation.
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ using DataFrames
|
|||||||
using Shapefile
|
using Shapefile
|
||||||
using Statistics
|
using Statistics
|
||||||
using ZipFile
|
using ZipFile
|
||||||
|
using ProgressBars
|
||||||
|
|
||||||
crc32 = crc(CRC_32)
|
crc32 = crc(CRC_32)
|
||||||
|
|
||||||
@@ -51,26 +52,27 @@ function download_census_gov(url, outputdir, expected_crc32)::Nothing
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
function load_2018_us_county()::Dict{String,GeoPoint}
|
function load_census_gov(;
|
||||||
db_name = "2018-us-county"
|
db_name,
|
||||||
|
url,
|
||||||
|
expected_crc32,
|
||||||
|
shp_filename,
|
||||||
|
extract_id,
|
||||||
|
)::Dict{String,GeoPoint}
|
||||||
basedir = joinpath(dirname(@__FILE__), "..", "..", "data", db_name)
|
basedir = joinpath(dirname(@__FILE__), "..", "..", "data", db_name)
|
||||||
csv_filename = "$basedir/locations.csv"
|
csv_filename = "$basedir/locations.csv"
|
||||||
if !isfile(csv_filename)
|
if !isfile(csv_filename)
|
||||||
download_census_gov(
|
download_census_gov(url, basedir, expected_crc32)
|
||||||
"https://www2.census.gov/geo/tiger/GENZ2018/shp/cb_2018_us_county_500k.zip",
|
@info "Processing: $shp_filename"
|
||||||
basedir,
|
table = Shapefile.Table(joinpath(basedir, shp_filename))
|
||||||
0x83eaec6d,
|
|
||||||
)
|
|
||||||
table = Shapefile.Table("$basedir/cb_2018_us_county_500k.shp")
|
|
||||||
geoms = Shapefile.shapes(table)
|
geoms = Shapefile.shapes(table)
|
||||||
df = DataFrame(id = String[], latitude = Float64[], longitude = Float64[])
|
df = DataFrame(id = String[], latitude = Float64[], longitude = Float64[])
|
||||||
for (i, geom) in enumerate(geoms)
|
for (i, geom) in tqdm(enumerate(geoms))
|
||||||
c = centroid(geom)
|
c = centroid(geom)
|
||||||
id = table.STATEFP[i] * table.COUNTYFP[i]
|
id = extract_id(table, i)
|
||||||
push!(df, [id, c.lat, c.lon])
|
push!(df, [id, c.lat, c.lon])
|
||||||
end
|
end
|
||||||
sort!(df)
|
sort!(df)
|
||||||
@info "Writing: $csv_filename"
|
|
||||||
CSV.write(csv_filename, df)
|
CSV.write(csv_filename, df)
|
||||||
end
|
end
|
||||||
if db_name ∉ keys(DB_CACHE)
|
if db_name ∉ keys(DB_CACHE)
|
||||||
@@ -81,8 +83,52 @@ function load_2018_us_county()::Dict{String,GeoPoint}
|
|||||||
return DB_CACHE[db_name]
|
return DB_CACHE[db_name]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function _id_2018_us_county(table::Shapefile.Table, i::Int)::String
|
||||||
|
return table.STATEFP[i] * table.COUNTYFP[i]
|
||||||
|
end
|
||||||
|
|
||||||
|
function load_2018_us_county()::Dict{String,GeoPoint}
|
||||||
|
return load_census_gov(
|
||||||
|
db_name = "2018-us-county",
|
||||||
|
url = "https://www2.census.gov/geo/tiger/GENZ2018/shp/cb_2018_us_county_500k.zip",
|
||||||
|
expected_crc32 = 0x83eaec6d,
|
||||||
|
shp_filename = "cb_2018_us_county_500k.shp",
|
||||||
|
extract_id = _id_2018_us_county,
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
function _id_2018_us_zcta(table::Shapefile.Table, i::Int)::String
|
||||||
|
return table.ZCTA5CE10[i]
|
||||||
|
end
|
||||||
|
|
||||||
|
function load_2018_us_zcta()::Dict{String,GeoPoint}
|
||||||
|
return load_census_gov(
|
||||||
|
db_name = "2018-us-zcta",
|
||||||
|
url = "https://www2.census.gov/geo/tiger/GENZ2018/shp/cb_2018_us_zcta510_500k.zip",
|
||||||
|
expected_crc32 = 0x6391f5fc,
|
||||||
|
shp_filename = "cb_2018_us_zcta510_500k.shp",
|
||||||
|
extract_id = _id_2018_us_zcta,
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
function _id_us_state(table::Shapefile.Table, i::Int)::String
|
||||||
|
return table.STUSPS[i]
|
||||||
|
end
|
||||||
|
|
||||||
|
function load_us_state()::Dict{String,GeoPoint}
|
||||||
|
return load_census_gov(
|
||||||
|
db_name = "us-state",
|
||||||
|
url = "https://www2.census.gov/geo/tiger/GENZ2018/shp/cb_2018_us_state_500k.zip",
|
||||||
|
expected_crc32 = 0x9469e5ca,
|
||||||
|
shp_filename = "cb_2018_us_state_500k.shp",
|
||||||
|
extract_id = _id_us_state,
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
function load_latlon_database(db_name)
|
function load_latlon_database(db_name)
|
||||||
db_name == "2018-us-county" && return load_2018_us_county()
|
db_name == "2018-us-county" && return load_2018_us_county()
|
||||||
|
db_name == "2018-us-zcta" && return load_2018_us_zcta()
|
||||||
|
db_name == "us-state" && return load_us_state()
|
||||||
error("Unknown database: $db_name")
|
error("Unknown database: $db_name")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -9,3 +9,15 @@ using RELOG
|
|||||||
@test point.lat == 41.83956
|
@test point.lat == 41.83956
|
||||||
@test point.lon == -88.08857
|
@test point.lon == -88.08857
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@testset "geodb_query (2018-us-zcta)" begin
|
||||||
|
point = RELOG.geodb_query("2018-us-zcta:60439")
|
||||||
|
@test point.lat == 41.68241
|
||||||
|
@test point.lon == -87.98954
|
||||||
|
end
|
||||||
|
|
||||||
|
@testset "geodb_query (us-state)" begin
|
||||||
|
point = RELOG.geodb_query("us-state:IL")
|
||||||
|
@test point.lat == 39.73939
|
||||||
|
@test point.lon == -89.50414
|
||||||
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user