############################
# MAPS OF SAMPLE AND CONFLICT EVENTS
#
#
# Part of replication files for:
# Dill, Janina, Howlett, Marnie, & Müller-Crepon, Carl (2025).
# Do Ukrainians Still Prefer Self-Defense Against Russia At Any Cost? 
# Journal of Peace Research, forthcoming.
#
#############################

# MAP OF SAMPLE ############

# Load Admin units
adm.shp <- readOGR("data/adm_shapes",
                   "gadm40_UKR_1",
                   verbose = F)

# Unify Kiev city
kiev <-  SpatialPolygonsDataFrame(gUnaryUnion(adm.shp[adm.shp$NAME_1 %in% c("Kiev City", "?" ),], id = rep(1,2)),
                                  adm.shp@data[adm.shp$NAME_1 == "Kiev City",],
                                  match.ID = F)
adm.shp <- rbind(adm.shp[!adm.shp$NAME_1 %in% c("Kiev City", "?" ) ,],
                 kiev)

# Encode includes
loc.df <- unique(data.long[data.long$experiment %in% c(1:2),
                    c("x_noise", "y_noise", "PSU", "in_2022", "in_2024")])
included <- gIntersects(adm.shp, 
                        SpatialPoints(loc.df[, c("x_noise", "y_noise")]),
                        byid = T)
included <- apply(included, 2, any)
included[adm.shp$NAME_1 == "Sumy"] <- FALSE ## Correct for randomly displaced PSU coordinate that crossed the border

# Map sample
png(filename = file.path(fig.path, "map_sample.png"),
    res = 300, units = "in", width = 6, height = 4)
par(mar = c(0,0,0,0))
plot(adm.shp)
plot(adm.shp[included,],
     col = "grey", add = T)
points(SpatialPoints(loc.df[loc.df$in_2022 == 1, c("x_noise", "y_noise")]),
       pch = 3)
points(SpatialPoints(loc.df[loc.df$in_2024 == 1, c("x_noise", "y_noise")]),
       pch = 4)
dev.off()


# Map war
png(filename = file.path(fig.path, "map_war.png"),
    res = 300, units = "in", width = 6, height = 4)
par(mar = c(0,0,0,0))
plot(adm.shp)
plot(adm.shp[included,],
     col = "grey", add = T)
points(SpatialPoints(acled.df[acled.df$date <= as.Date("2024-12-31"), c("LONGITUDE", "LATITUDE")]),
       pch = 19, col = rgb(1,0,0,.01))
dev.off()


# # Map war
# pdf(file = file.path(fig.path, "fig1b_map_war.pdf"),
#     width = 6, height = 4)
# par(mar = c(0,0,0,0))
# plot(adm.shp)
# plot(adm.shp[included,],
#      col = "grey", add = T)
# points(SpatialPoints(acled.df[, c("LONGITUDE", "LATITUDE")]),
#        pch = 19, col = rgb(1,0,0,.1))
# dev.off()



