Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
vwmaus committed Jan 23, 2020
2 parents cdfdcfd + 551b70f commit 1285101
Show file tree
Hide file tree
Showing 9 changed files with 11,352 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app_edit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# About this app

This app is meant as an application to edit an existing set of polygons,
to check a part of it (defined with "to_check" TRUE) by editing/deleting/etc them,
and then to store them in a new db.
119 changes: 119 additions & 0 deletions app_edit/db/edit_model.dbm
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
CAUTION: Do not modify this file unless you know what you are doing.
Unexpected results may occur if the code is changed deliberately.
-->
<dbmodel pgmodeler-ver="0.9.2" last-position="0,0" last-zoom="0.7" max-obj-count="4"
default-schema="public" default-owner="postgres">
<role name="app"
login="true"
encrypted="true"
password="v&amp;@MMcU7g4mb">
<prepended-sql><![CDATA[DROP ROLE IF EXISTS app;]]></prepended-sql>
</role>

<database name="vector_mine_edit" is-template="false" allow-conns="true" append-at-eod="true">
<role name="postgres"/>
<appended-sql><![CDATA[GRANT CONNECT ON DATABASE vector_mine_edit TO app;
GRANT SELECT, UPDATE ON to_update TO app;
GRANT SELECT, INSERT ON updated TO app;
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO app;]]></appended-sql>
</database>

<schema name="public" layer="0" fill-color="#e1e1e1" sql-disabled="true">
</schema>

<extension name="postgis">
</extension>

<table name="to_update" layer="0" collapse-mode="2" max-obj-count="4">
<schema name="public"/>
<role name="postgres"/>
<position x="100" y="80"/>
<column name="id" not-null="true">
<type name="bigserial" length="0"/>
</column>
<column name="geometry" not-null="true">
<type name="geometry" length="0" spatial-type="POLYGON" variation="0" srid="0"/>
</column>
<column name="to_check" not-null="true">
<type name="boolean" length="0"/>
</column>
<column name="status" not-null="true">
<type name="varchar" length="10"/>
</column>
<constraint name="to_update_pk" type="pk-constr" table="public.to_update">
<columns names="id" ref-type="src-columns"/>
</constraint>
</table>

<index name="idx_to_update" table="public.to_update"
concurrent="false" unique="false" fast-update="false" buffering="false"
index-type="btree" factor="0">
<idxelement use-sorting="false">
<column name="id"/>
</idxelement>
</index>

<index name="idx_to_update_geom_poly" table="public.to_update"
concurrent="false" unique="false" fast-update="false" buffering="false"
index-type="gist" factor="0">
<idxelement use-sorting="false">
<column name="geometry"/>
</idxelement>
</index>

<table name="updated" layer="0" collapse-mode="2" max-obj-count="3">
<schema name="public"/>
<role name="postgres"/>
<position x="561.25" y="86.25"/>
<column name="id" not-null="true">
<type name="bigserial" length="0"/>
</column>
<column name="geometry">
<type name="geometry" length="0" spatial-type="MULTIPOLYGON" variation="0" srid="0"/>
</column>
<constraint name="updated_pk" type="pk-constr" table="public.updated">
<columns names="id" ref-type="src-columns"/>
</constraint>

<customidxs object-type="column">
<object name="id_to_update" index="2"/>
</customidxs>

<customidxs object-type="constraint">
<object name="to_update_fk" index="1"/>
</customidxs>
</table>

<index name="idx_updated" table="public.updated"
concurrent="false" unique="false" fast-update="false" buffering="false"
index-type="btree" factor="0">
<idxelement use-sorting="false">
<column name="id"/>
</idxelement>
</index>

<index name="idx_updated_geom_poly" table="public.updated"
concurrent="false" unique="false" fast-update="false" buffering="false"
index-type="gist" factor="0">
<idxelement use-sorting="false">
<column name="geometry"/>
</idxelement>
</index>

<relationship name="to_update_has_many_updated" type="rel1n" layer="0"
src-col-pattern="{sc}_{st}"
pk-pattern="{dt}_pk" uq-pattern="{dt}_uq"
src-fk-pattern="{st}_fk"
custom-color="#a57d0d"
src-table="public.to_update"
dst-table="public.updated"
src-required="true" dst-required="false"/>

<textbox name="Note status" layer="0" font-size="9">
<position x="80" y="267.5"/>
<comment><![CDATA[status: DONT_CHECK, TO_CHECK, CHECKED, HOLD]]></comment>
</textbox>

</dbmodel>
42 changes: 42 additions & 0 deletions app_edit/db/insert_mines_to_update.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
library(sf)
library(DBI)
library(tidyverse)

# --------------------------------------------------------------------------------------
# connect PostGIS mine vector database -------------------------------------------------
conn <- DBI::dbConnect(
drv = RPostgreSQL::PostgreSQL(),
host = Sys.getenv("db_host"),
port = Sys.getenv("db_port"),
dbname = Sys.getenv("db_name"),
user = Sys.getenv("db_admin_user"),
password = Sys.getenv("db_admin_password"),
)

# --------------------------------------------------------------------------------------
# read mines from files ----------------------------------------------------------------
to_update_tbl <- sf::st_read(dsn = "./app_edit/db/polygons_to_check_v1.gpkg",
stringsAsFactors = FALSE) %>%
dplyr::select(geom, to_check) %>%
dplyr::rename("geometry" = "geom") %>%
dplyr::mutate(status = if_else(to_check, "TO_CHECK", "DONT_CHECK"))

# --------------------------------------------------------------------------------------
# feed polygons to check to database ---------------------------------------------------
sf::st_write(obj = to_update_tbl,
dsn = conn,
layer = "to_update",
append = TRUE,
factorsAsCharacter = TRUE)

# --------------------------------------------------------------------------------------
# run vacuun analyze -------------------------------------------------------------------
DBI::dbSendQuery(conn, statement = "VACUUM ANALYZE to_update;")

# --------------------------------------------------------------------------------------
# close database connection ------------------------------------------------------------
DBI::dbDisconnect(conn)

# --------------------------------------------------------------------------------------
# remove all variables from environment ------------------------------------------------
rm(list=ls())
132 changes: 132 additions & 0 deletions app_edit/global.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
### global.R
# This file is loaded on startup of the app.
# Here, code that only needs to be performed once is executed.
# Typically, this includes defining libraries,
# setting up the database connection, and
# defining variables connected to database operation.

## STRUCTURE
# 1. Load packages
# 2. Data setup
# 3. Functions

##################################################################
### 1. Load packages
##################################################################

library(shiny)
library(tidyverse)
library(sf)
# devtools::install_github("Gutschlhofer/mapedit", force = TRUE)
library(mapedit) # make sure the modified version of mapedit is used
library(shiny)
library(shinyjs)
library(httr)
library(DBI)
library(pool)
library(lwgeom)

##################################################################
### 2. Data setup
##################################################################

# connect PostGIS database via a pool object for better Shiny behaviour ------------------------------------------------
pool <- pool::dbPool(
drv = RPostgreSQL::PostgreSQL(),
host = Sys.getenv("db_host"),
port = Sys.getenv("db_port"),
dbname = Sys.getenv("db_name"),
user = Sys.getenv("db_user"),
password = Sys.getenv("db_password"),
minSize = 1,
maxSize = Inf,
idleTimeout = 300,
validationInterval = 120
)
onStop(function() { # this is required to close the pool so we have no leaking connections
pool::poolClose(pool)
message("Pool closed.")
})

##################################################################
### 3. Functions
##################################################################

get_next_polygon <- function(){
next_poly <- sf::st_read(dsn = pool,
layer = NULL,
query = sprintf("SELECT * FROM to_update
WHERE status = 'TO_CHECK' AND id NOT IN
(SELECT id_to_update FROM updated) LIMIT 1;"))

if(is.null(next_poly) | nrow(next_poly) == 0){
return("You are done updating the polygons. Congratulations!
Please get in contact with your supervisor!")
}

# NOW, put on hold

## TODO

return(next_poly)
}

get_adjacent_polygons <- function(current_poly = NULL, old = TRUE){
if(is.null(current_poly)){
rlang::abort(message = "current_poly must be specified.")
}

if(old){
# NOW, provide all polygons (except the one in question)
## 1. get the ones in to_update
polygons_1 <- sf::st_read(dsn = pool,
layer = NULL,
query = sprintf('SELECT * FROM to_update
WHERE NOT id = %.0f AND
NOT id IN (SELECT id_to_update FROM updated) AND
ST_DWithin(geometry,
ST_GeomFromText(\'SRID=4326; %s\'),
50000, TRUE)',
current_poly$id[1],
sf::st_as_text(current_poly$geometry)[1]))

if(nrow(polygons_1) > 0){
poly <- sf::st_combine(polygons_1$geometry)
} else {
poly <- sf::st_multipolygon() %>%
sf::st_sfc() %>%
sf::st_sf()

sf::st_crs(poly$geometry) <- "+proj=longlat +datum=WGS84 +no_defs"
}
} else {
## 2. get the ones in updated
polygons_2 <- sf::st_read(dsn = pool,
layer = NULL,
query = sprintf('SELECT * FROM updated
WHERE ST_DWithin(geometry,
ST_GeomFromText(\'SRID=4326; %s\'),
50000, TRUE)',
sf::st_as_text(current_poly$geometry)[1]))

if(nrow(polygons_2) > 0){
poly <- sf::st_combine(polygons_2$geometry)
} else {
poly <- sf::st_multipolygon() %>%
sf::st_sfc() %>%
sf::st_sf()

sf::st_crs(poly$geometry) <- "+proj=longlat +datum=WGS84 +no_defs"
}
}

return(poly)
}

# https://github.com/pointhi/leaflet-color-markers
get_leaflet_marker_url <- function(color = "blue"){
return(sprintf("https://cdn.rawgit.com/pointhi/leaflet-color-markers/master/img/marker-icon-2x-%s.png", color))
}

# allow bookmarking (necessary to allow saving the state of the app in a URL)
enableBookmarking(store = "url")
Loading

0 comments on commit 1285101

Please sign in to comment.