Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Karmana update #45

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions src/Karmana.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ function __init__()
district_df[][302, :HR_Nmbr] = 3 # Kinnaur - district name not assigned HR_Name
district_df[][413, :HR_Nmbr] = 3 # North Sikkim - district not assigned HR_Name nor district name # 104
end
hr_df[] = _prepare_merged_geom_dataframe(district_df[], :HR_Nmbr, :ST_NM; capture_fields = (:ST_NM, :ST_CD, :HR_Name))
hr_df[] = _prepare_merged_geom_dataframe(district_df[], :HR_Nmbr, :ST_NM; capture_fields = (:ST_NM, :ST_CD, :HR_Nmbr))
state_df[] = _prepare_merged_geom_dataframe(district_df[], :ST_NM; capture_fields = (:ST_CD,))

# finally, patch the loaded dataframes, to match the maps database
Expand All @@ -147,20 +147,16 @@ function __init__()
has_india_data = true

elseif has_maps_db_config
try
_state_df, _hr_df, _district_df = state_hr_district_dfs()

# _district_df[302, :hr_nmbr] = 3 # Kinnaur - district name not assigned HR_Name
# _district_df[413, :hr_nmbr] = 3 # North Sikkim - district not assigned HR_Name nor district name
state_df[] = _state_df
hr_df[] = _hr_df
district_df[] = _district_df
has_india_data = true
catch e
@warn "Failed to load data from maps database. Falling back to shapefile."
@warn e
has_india_data = false
end
# try
_district_df = state_hr_district_dfs()
district_df[] = _district_df
district_df[].geometry = GeoInterface.convert.((GeometryBasics,), district_df[].geometry)
# apply certain patches here, if needed
district_df[][302, :hr_nmbr] = 3 # Kinnaur - district name not assigned HR_Name
district_df[][413, :hr_nmbr] = 3 # North Sikkim - district not assigned HR_Name nor district name # 104
hr_df[] = _prepare_merged_geom_dataframe(district_df[], :hr_nmbr, :st_nm; capture_fields = (:st_nm, :st_cd, :hr_name))
state_df[] = _prepare_merged_geom_dataframe(district_df[], :st_nm; capture_fields = (:st_cd,))
hr_df[].hr_nmbr_str = ("HR ",) .* string.(hr_df[].hr_nmbr)
end

if !has_india_data # no shapefile path, no maps db access ⟹ use the artifact!
Expand Down
14 changes: 5 additions & 9 deletions src/data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,26 +84,22 @@ function state_hr_district_dfs()
spatial_ref_df = DBInterface.execute(connection, "SELECT * from spatial_ref_sys") |> DataFrame
db_crs = spatial_ref_df.SRTEXT[begin] # TODO: use this when converting.
# get tables for each admin level
state_table = do_geoquery(connection, "states")
hr_table = do_geoquery(connection, "homogeneous_regions")
district_table = do_geoquery(connection, "districts_2011")
district_table = do_geoquery(connection,"districts_states_hr" )
# get rivers etc
shape_wkb_to_module_geom!(GeometryBasics, state_table)
shape_wkb_to_module_geom!(GeometryBasics, hr_table)
shape_wkb_to_module_geom!(GeometryBasics, district_table)
# drop missing values, since we shouldn't have any at this stage.
dropmissing!(state_table)
dropmissing!(hr_table)
dropmissing!(district_table)
# clean up by closing the connection
DBInterface.close!(connection)

return state_table, hr_table, district_table
return district_table

end

# example code in R whose functionality this replicates
# read_the_map = st_read(dsn=dsn_for_maps, layer = aggregation.layer)

# TODO: figure out why this doesn't work
# dataset = ArchGDAL.read("MySQL:maps;host=data.mayin.org;user=$(maps_user);password=$(maps_password)")
# dataset = ArchGDAL.read("MySQL:maps;host=data.mayin.org;user=$(maps_user);password=$(maps_password)")
# my name is siddhant

12 changes: 5 additions & 7 deletions src/plotting/indiaoutline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function id_key_for_admin_level(admin_level::Symbol)
elseif admin_level == :HR
:hr_nmbr
elseif admin_level == :District
:dt_cen_cd
:censuscode
else
@error("The admin code `$admin_code` which you passed is invalid. Valid admin codes are `:State`, `:HR`, and `:District`.")
end
Expand Down Expand Up @@ -261,16 +261,14 @@ function Makie.plot!(plot::IndiaOutline)
end
notify(plot.converted[2])

state_plot = poly!(plot, plot.State, state_geoms; color = state_colors, colormap = plot.colormap, highclip = plot.highclip, lowclip = plot.lowclip)
hr_plot = poly!(plot, hr_geoms; color = hr_colors, colorrange = get(plot, :colorrange, (0, 1)), colormap = plot.colormap, highclip = plot.highclip, lowclip = plot.lowclip, plot.HR...)
district_plot = poly!(plot, district_geoms; color = district_colors, colormap = plot.colormap, colorrange = get(plot, :colorrange, (0, 1)), highclip = plot.highclip, lowclip = plot.lowclip, plot.District...)
river_plot = lines!(plot, GeoInterface.convert(GeometryBasics, Karmana.india_rivers[]); inspectable = false, xautolimits = false, yautolimits = false, plot.River...)

state_plot = poly!(plot, plot.State, state_geoms; color = state_colors, colormap = plot.colormap)
hr_plot = poly!(plot, hr_geoms; color = hr_colors, colorrange = get(plot, :colorrange, (0, 1)), colormap = plot.colormap, plot.HR...)
district_plot = poly!(plot, district_geoms; color = district_colors, colormap = plot.colormap, colorrange = get(plot, :colorrange, (0, 1)), plot.District...)


on(Base.Fix1(_set_plot_z, state_plot), plot.State.zlevel; update = true)
on(Base.Fix1(_set_plot_z, hr_plot), plot.HR.zlevel; update = true)
on(Base.Fix1(_set_plot_z, district_plot), plot.District.zlevel; update = true)
on(Base.Fix1(_set_plot_z, river_plot), plot.River.zlevel; update = true)

return plot
end
Expand Down
14 changes: 8 additions & 6 deletions src/readnl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ start_date = Date(2015, 01)
end_date = Date(2020, 12)
rad_dc, cf_dc = readnl(xlims, ylims, start_date, end_date)
"""
function readnl(xlims = X(Rasters.Between(65.39, 99.94)), ylims = Y(Rasters.Between(5.34, 39.27)), start_date = Date(2012, 04), end_date = Date(2023, 01))
function readnl(xlims = X(Rasters.Between(65.39, 99.94)), ylims = Y(Rasters.Between(5.34, 39.27)), start_date = Date(2012, 04), end_date = Date(today()))
lims = xlims, ylims
rad_path = "/mnt/giant-disk/nighttimelights/monthly/rad/"
rad_files, sorted_dates = sort_files_by_date(rad_path, start_date, end_date)
Expand All @@ -48,8 +48,9 @@ function readnl(xlims = X(Rasters.Between(65.39, 99.94)), ylims = Y(Rasters.Betw
cf_raster_list = [Raster(i, lazy = true)[lims...] for i in cf_path .* cf_files]
rad_series = RasterSeries(rad_raster_list, Ti(sorted_dates))
cf_series = RasterSeries(cf_raster_list, Ti(sorted_dates))
rad_datacube = Rasters.combine(rad_series, Ti)
cf_datacube = Rasters.combine(cf_series, Ti)
rad_datacube = Float16.(Rasters.combine(rad_series, Ti))
cf_datacube = Int8.(Rasters.combine(cf_series, Ti))
replace!(rad_datacube, Inf => prevfloat(typemax(rad_datacube[1,1,1])))
return rad_datacube, cf_datacube
end

Expand All @@ -75,7 +76,7 @@ end_date = Date(2020, 12)
rad_dc, cf_dc = readnl(geom, start_date, end_date)
```
"""
function readnl(geom, start_date = Date(2012, 04), end_date = Date(2023, 01))
function readnl(geom, start_date = Date(2012, 04), end_date = today())
rad_path = "/mnt/giant-disk/nighttimelights/monthly/rad/"
rad_files, sorted_dates = sort_files_by_date(rad_path, start_date, end_date)
cf_path = "/mnt/giant-disk/nighttimelights/monthly/cf/"
Expand All @@ -84,7 +85,8 @@ function readnl(geom, start_date = Date(2012, 04), end_date = Date(2023, 01))
cf_raster_list = [crop(Raster(i, lazy = true), to = geom) for i in cf_path .* cf_files]
rad_series = RasterSeries(rad_raster_list, Ti(sorted_dates))
cf_series = RasterSeries(cf_raster_list, Ti(sorted_dates))
rad_datacube = Rasters.combine(rad_series, Ti)
cf_datacube = Rasters.combine(cf_series, Ti)
rad_datacube = Float16.(Rasters.combine(rad_series, Ti))
cf_datacube = Int8.(Rasters.combine(cf_series, Ti))
replace!(rad_datacube, Inf => prevfloat(typemax(rad_datacube[1,1,1])))
return rad_datacube, cf_datacube
end
Loading