Skip to content

Commit

Permalink
Merge pull request #98 from GispoCoding/Lahti-160-ymparistomuuttujien…
Browse files Browse the repository at this point in the history
…-luku

Lahti: ympäristmuuttujien luku
  • Loading branch information
Mtk112 authored Mar 15, 2024
2 parents 260199b + 000d567 commit ed29143
Show file tree
Hide file tree
Showing 17 changed files with 375 additions and 119 deletions.
2 changes: 2 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ JKR_PASSWORD=qwerty
JKR_TEST_DB=jkr_test
JKR_TEST_DB_PORT=5436
JKR_TEST_PASSWORD=qwerty

QGIS_BIN_PATH=C:\\Program Files\\QGIS 3.28.11\\bin
14 changes: 13 additions & 1 deletion jkrimporter/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,30 @@

from dotenv import dotenv_values

from tests.conftest import TEST_ENV


# Path to the .env file in the user's %APPDATA%/jkr directory
dotenv_path = os.path.join(os.getenv('APPDATA'), 'jkr', '.env')

# Read the environment variables from the .env file
env = {
**dotenv_values(".env"),
**dotenv_values(dotenv_path),
**os.environ,
}

# Define database configuration using environment variables
dbconf = {
"host": env.get("JKR_DB_HOST", None),
"port": env.get("JKR_DB_PORT", None),
"username": env.get("JKR_USER", None),
"password": env.get("JKR_PASSWORD", None),
"dbname": env.get("JKR_DB", None),
}
if TEST_ENV:
dbconf["port"] = env.get("JKR_TEST_DB_PORT", None)
dbconf["password"] = env.get("JKR_TEST_PASSWORD", None)
dbconf["dbname"] = env.get("JKR_TEST_DB", None)

__all__ = ["dbconf"]

Expand Down
40 changes: 34 additions & 6 deletions scripts/drop_schemas.bat
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,39 @@ CHCP 65001
REM Kerrotaan Postgresille myös että terminaalin encoding on UTF-8
SET PGCLIENTENCODING=UTF8

SET HOST=localhost
SET PORT=5435
SET DB_NAME=jkr
SET USER=jkr_admin
REM Määritä salasana %APPDATA%\postgresql\pgpass.conf tiedostossa
REM Tarkistetaan .env tiedosto.
if not exist "%APPDATA%\jkr\.env" (
echo Error: .env file not found at %APPDATA%\jkr\.env
exit /b 1
)

REM Ladataan muuttujat .env tiedostosta.
for /f "usebackq tokens=1,* delims==" %%a in ("%APPDATA%\jkr\.env") do (
set "%%a=%%b"
)

REM Tarkistetaan onko tarvittavat muuttujat asetettu.
if "%JKR_DB_HOST%"=="" (
echo Error: HOST variable not set in .env file
exit /b 1
)
if "%JKR_DB_PORT%"=="" (
echo Error: PORT variable not set in .env file
exit /b 1
)
if "%JKR_DB%"=="" (
echo Error: DB_NAME variable not set in .env file
exit /b 1
)
if "%JKR_USER%"=="" (
echo Error: USER variable not set in .env file
exit /b 1
)
if "%QGIS_BIN_PATH%"=="" (
echo Error: QGIS_BIN_PATH variable not set in .env file
exit /b 1
)


ECHO Muunnetaan jkr-muotoon...
psql -h %HOST% -p %PORT% -d %DB_NAME% -U %USER% -f "drop_schemas.sql"
"%QGIS_BIN_PATH%\\psql" -h %JKR_DB_HOST% -p %JKR_DB_PORT% -d %JKR_DB% -U %JKR_USER% -f "drop_schemas.sql"
61 changes: 41 additions & 20 deletions scripts/import_and_create_kohteet.bat
Original file line number Diff line number Diff line change
@@ -1,7 +1,40 @@
@echo off

setlocal

REM Tarkistetaan .env tiedosto.
if not exist "%APPDATA%\jkr\.env" (
echo Error: .env file not found at %APPDATA%\jkr\.env
exit /b 1
)

REM Ladataan muuttujat .env tiedostosta.
for /f "usebackq tokens=1,* delims==" %%a in ("%APPDATA%\jkr\.env") do (
set "%%a=%%b"
)

REM Tarkistetaan onko tarvittavat muuttujat asetettu.
if "%JKR_DB_HOST%"=="" (
echo Error: HOST variable not set in .env file
exit /b 1
)
if "%JKR_DB_PORT%"=="" (
echo Error: PORT variable not set in .env file
exit /b 1
)
if "%JKR_DB%"=="" (
echo Error: DB_NAME variable not set in .env file
exit /b 1
)
if "%JKR_USER%"=="" (
echo Error: USER variable not set in .env file
exit /b 1
)
if "%QGIS_BIN_PATH%"=="" (
echo Error: QGIS_BIN_PATH variable not set in .env file
exit /b1
)


REM Tarkistetaan, että dvv-aineisto on syötetty
IF "%~1"=="" (
echo Anna dvv-aineiston tiedostopolku.
Expand Down Expand Up @@ -35,38 +68,26 @@ CHCP 65001
REM Kerrotaan Postgresille myös että terminaalin encoding on UTF-8
SET PGCLIENTENCODING=UTF8

SET HOST=<palvelimen nimi>
SET PORT=<tietokantaportti>
SET DB_NAME=<tietokannan_nimi>
SET USER=<kayttajatunnus>
REM Määritä salasana %APPDATA%\postgresql\pgpass.conf tiedostossa

REM Määritä polku QGISin psql asennukseen. Esim "C:\\Program Files\\QGIS 3.30.2\\bin".
SET PSQL_PATH="<Tiedostopolku QGISin bin kansioon>"

REM Määritä polku QGISin ogr2ogr asennukseen. Esim "C:\\Program Files\\QGIS 3.30.2\\bin".
SET OGR2OGR_PATH="<Tiedostopolku QGISin bin kansioon>"

REM Tarkistetaan halutaanko importoida posti data.

IF "%~3"=="posti" (
%PSQL_PATH%\\psql -h %HOST% -p %PORT% -d %DB_NAME% -U %USER% -f "./scripts/jkr_posti.sql"
"%QGIS_BIN_PATH%\\psql" -h %JKR_DB_HOST% -p %JKR_DB_PORT% -d %JKR_DB% -U %JKR_USER% -f "./scripts/jkr_posti.sql"
)

ECHO Rakennukset
%OGR2OGR_PATH%\\ogr2ogr -f PostgreSQL -overwrite -progress PG:"host=%HOST% port=%PORT% dbname=%DB_NAME% user=%USER% ACTIVE_SCHEMA=jkr_dvv" -nln rakennus %DVV% "R1 rakennus"
"%QGIS_BIN_PATH%\\ogr2ogr" -f PostgreSQL -overwrite -progress PG:"host=%JKR_DB_HOST% port=%JKR_DB_PORT% dbname=%JKR_DB% user=%JKR_USER% ACTIVE_SCHEMA=jkr_dvv" -nln rakennus %DVV% "R1 rakennus"

ECHO Osoitteet
%OGR2OGR_PATH%\\ogr2ogr -f PostgreSQL -overwrite -progress PG:"host=%HOST% port=%PORT% dbname=%DB_NAME% user=%USER% ACTIVE_SCHEMA=jkr_dvv" -nln osoite %DVV% "R3 osoite"
"%QGIS_BIN_PATH%\\ogr2ogr" -f PostgreSQL -overwrite -progress PG:"host=%JKR_DB_HOST% port=%JKR_DB_PORT% dbname=%JKR_DB% user=%JKR_USER% ACTIVE_SCHEMA=jkr_dvv" -nln osoite %DVV% "R3 osoite"

ECHO Omistajat
%OGR2OGR_PATH%\\ogr2ogr -f PostgreSQL -overwrite -progress PG:"host=%HOST% port=%PORT% dbname=%DB_NAME% user=%USER% ACTIVE_SCHEMA=jkr_dvv" -nln omistaja %DVV% "R4 omistaja"
"%QGIS_BIN_PATH%\\ogr2ogr" -f PostgreSQL -overwrite -progress PG:"host=%JKR_DB_HOST% port=%JKR_DB_PORT% dbname=%JKR_DB% user=%JKR_USER% ACTIVE_SCHEMA=jkr_dvv" -nln omistaja %DVV% "R4 omistaja"

ECHO Asukkaat
%OGR2OGR_PATH%\\ogr2ogr -f PostgreSQL -overwrite -progress PG:"host=%HOST% port=%PORT% dbname=%DB_NAME% user=%USER% ACTIVE_SCHEMA=jkr_dvv" -nln vanhin %DVV% "R9 huon asukk"
"%QGIS_BIN_PATH%\\ogr2ogr" -f PostgreSQL -overwrite -progress PG:"host=%JKR_DB_HOST% port=%JKR_DB_PORT% dbname=%JKR_DB% user=%JKR_USER% ACTIVE_SCHEMA=jkr_dvv" -nln vanhin %DVV% "R9 huon asukk"

ECHO Muunnetaan jkr-muotoon...

%PSQL_PATH%\\psql -h %HOST% -p %PORT% -d %DB_NAME% -U %USER% -v formatted_date="%formatted_date%" -f "./scripts/import_dvv.sql"
"%QGIS_BIN_PATH%\\psql" -h %JKR_DB_HOST% -p %JKR_DB_PORT% -d %JKR_DB% -U %JKR_USER% -v formatted_date="%formatted_date%" -f "./scripts/import_dvv.sql"

endlocal
endlocal
47 changes: 37 additions & 10 deletions scripts/import_dvv.bat
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,50 @@ CHCP 65001
REM Kerrotaan Postgresille myös että terminaalin encoding on UTF-8
SET PGCLIENTENCODING=UTF8

SET HOST=localhost
SET PORT=5435
SET DB_NAME=jkr
SET USER=jkr_admin
REM Määritä salasana %APPDATA%\postgresql\pgpass.conf tiedostossa
REM Tarkistetaan .env tiedosto.
if not exist "%APPDATA%\jkr\.env" (
echo Error: .env file not found at %APPDATA%\jkr\.env
exit /b 1
)

REM Ladataan muuttujat .env tiedostosta.
for /f "usebackq tokens=1,* delims==" %%a in ("%APPDATA%\jkr\.env") do (
set "%%a=%%b"
)

REM Tarkistetaan onko tarvittavat muuttujat asetettu.
if "%JKR_DB_HOST%"=="" (
echo Error: HOST variable not set in .env file
exit /b 1
)
if "%JKR_DB_PORT%"=="" (
echo Error: PORT variable not set in .env file
exit /b 1
)
if "%JKR_DB%"=="" (
echo Error: DB_NAME variable not set in .env file
exit /b 1
)
if "%JKR_USER%"=="" (
echo Error: USER variable not set in .env file
exit /b 1
)
if "%QGIS_BIN_PATH%"=="" (
echo Error: QGIS_BIN_PATH variable not set in .env file
exit /b1
)

ECHO Rakennukset
ogr2ogr -f PostgreSQL -overwrite -progress PG:"host=%HOST% port=%PORT% dbname=%DB_NAME% user=%USER% ACTIVE_SCHEMA=jkr_dvv" -nln rakennus "../data/dvv/DVV_rakennukset.xlsx" "R1 rakennus"
"%QGIS_BIN_PATH%\\ogr2ogr" -f PostgreSQL -overwrite -progress PG:"host=%JKR_DB_HOST% port=%JKR_DB_PORT% dbname=%JKR_DB% user=%JKR_USER% ACTIVE_SCHEMA=jkr_dvv" -nln rakennus "../tests/data/test_data_import/DVV_original.xlsx" "R1 rakennus"

ECHO Osoitteet
ogr2ogr -f PostgreSQL -overwrite -progress PG:"host=%HOST% port=%PORT% dbname=%DB_NAME% user=%USER% ACTIVE_SCHEMA=jkr_dvv" -nln osoite "../data/dvv/DVV_rakennukset.xlsx" "R3 osoite"
"%QGIS_BIN_PATH%\\ogr2ogr" -f PostgreSQL -overwrite -progress PG:"host=%JKR_DB_HOST% port=%JKR_DB_PORT% dbname=%JKR_DB% user=%JKR_USER% ACTIVE_SCHEMA=jkr_dvv" -nln osoite "../tests/data/test_data_import/DVV_original.xlsx" "R3 osoite"

ECHO Omistajat
ogr2ogr -f PostgreSQL -overwrite -progress PG:"host=%HOST% port=%PORT% dbname=%DB_NAME% user=%USER% ACTIVE_SCHEMA=jkr_dvv" -nln omistaja "../data/dvv/DVV_rakennukset.xlsx" "R4 omistaja"
"%QGIS_BIN_PATH%\\ogr2ogr" -f PostgreSQL -overwrite -progress PG:"host=%JKR_DB_HOST% port=%JKR_DB_PORT% dbname=%JKR_DB% user=%JKR_USER% ACTIVE_SCHEMA=jkr_dvv" -nln omistaja "../tests/data/test_data_import/DVV_original.xlsx" "R4 omistaja"

ECHO Asukkaat
ogr2ogr -f PostgreSQL -overwrite -progress PG:"host=%HOST% port=%PORT% dbname=%DB_NAME% user=%USER% ACTIVE_SCHEMA=jkr_dvv" -nln vanhin "../data/dvv/DVV_rakennukset.xlsx" "R9 huon asukk"
"%QGIS_BIN_PATH%\\ogr2ogr" -f PostgreSQL -overwrite -progress PG:"host=%JKR_DB_HOST% port=%JKR_DB_PORT% dbname=%JKR_DB% user=%JKR_USER% ACTIVE_SCHEMA=jkr_dvv" -nln vanhin "../tests/data/test_data_import/DVV_original.xlsx" "R9 huon asukk"

ECHO Muunnetaan jkr-muotoon...
psql -h %HOST% -p %PORT% -d %DB_NAME% -U %USER% -v formatted_date="%formatted_date%" -f "import_dvv.sql"
"%QGIS_BIN_PATH%\\psql" -h %JKR_DB_HOST% -p %JKR_DB_PORT% -d %JKR_DB% -U %JKR_USER% -v formatted_date="%formatted_date%" -f "import_dvv.sql"
39 changes: 33 additions & 6 deletions scripts/import_posti.bat
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,42 @@ CHCP 65001
REM Kerrotaan Postgresille myös että terminaalin encoding on UTF-8
SET PGCLIENTENCODING=UTF8

SET HOST=localhost
SET PORT=5435
SET DB_NAME=jkr
SET USER=jkr_admin
REM Määritä salasana %APPDATA%\postgresql\pgpass.conf tiedostossa
REM Tarkistetaan .env tiedosto.
if not exist "%APPDATA%\jkr\.env" (
echo Error: .env file not found at %APPDATA%\jkr\.env
exit /b 1
)

REM Ladataan muuttujat .env tiedostosta.
for /f "usebackq tokens=1,* delims==" %%a in ("%APPDATA%\jkr\.env") do (
set "%%a=%%b"
)

REM Tarkistetaan onko tarvittavat muuttujat asetettu.
if "%JKR_DB_HOST%"=="" (
echo Error: HOST variable not set in .env file
exit /b 1
)
if "%JKR_DB_PORT%"=="" (
echo Error: PORT variable not set in .env file
exit /b 1
)
if "%JKR_DB%"=="" (
echo Error: DB_NAME variable not set in .env file
exit /b 1
)
if "%JKR_USER%"=="" (
echo Error: USER variable not set in .env file
exit /b 1
)
if "%QGIS_BIN_PATH%"=="" (
echo Error: QGIS_BIN_PATH variable not set in .env file
exit /b 1
)

ECHO Kunnat ja postinumerot
REM # Kunnat ja postinumerot on tuotava tietokantaan ennen dvv-aineiston tuontia
psql -h %HOST% -p %PORT% -d %DB_NAME% -U %USER% -f "import_posti.sql"
"%QGIS_BIN_PATH%\psql" -h %JKR_DB_HOST% -p %JKR_DB_PORT% -d %JKR_DB% -U %JKR_USER% -f "import_posti.sql"

REM Kadut
REM Katuja ei tarvitse tuoda; tarvittavat kadut löytyvät dvv-osoiteaineistosta
Expand Down
44 changes: 34 additions & 10 deletions scripts/import_taajama.bat
Original file line number Diff line number Diff line change
@@ -1,6 +1,39 @@
@echo off
setlocal

REM Tarkistetaan .env tiedosto.
if not exist "%APPDATA%\jkr\.env" (
echo Error: .env file not found at %APPDATA%\jkr\.env
exit /b 1
)

REM Ladataan muuttujat .env tiedostosta.
for /f "usebackq tokens=1,* delims==" %%a in ("%APPDATA%\jkr\.env") do (
set "%%a=%%b"
)

REM Tarkistetaan onko tarvittavat muuttujat asetettu.
if "%JKR_DB_HOST%"=="" (
echo Error: HOST variable not set in .env file
exit /b 1
)
if "%JKR_DB_PORT%"=="" (
echo Error: PORT variable not set in .env file
exit /b 1
)
if "%JKR_DB%"=="" (
echo Error: DB_NAME variable not set in .env file
exit /b 1
)
if "%JKR_USER%"=="" (
echo Error: USER variable not set in .env file
exit /b 1
)
if "%QGIS_BIN_PATH%"=="" (
echo Error: QGIS_BIN_PATH variable not set in .env file
exit /b1
)

REM Tarkistetaan parametrit
IF "%~3"=="" (
ECHO Anna parametrit järjestyksessä
Expand All @@ -16,12 +49,6 @@ CHCP 65001
REM Kerrotaan Postgresille myäs että terminaalin encoding on UTF-8
SET PGCLIENTENCODING=UTF8

SET HOST=<palvelimen nimi>
SET PORT=<tietokantaportti>
SET DB_NAME=<tietokannan_nimi>
SET USER=<kayttajatunnus>
REM Määritä salasana %APPDATA%\postgresql\pgpass.conf tiedostossa

SET SHP_FILE=%~1
SET DATE_FROM=%~2
SET POPULATION=%~3
Expand All @@ -30,9 +57,6 @@ for %%A in ("%SHP_FILE%") do (
SET "SHP_TABLE=%%~nA"
)

REM Määritä polku QGISin ogr2ogr-asennukseen. Esim. "C:\\Program Files\\QGIS 3.30.2\\bin".
SET OGR2OGR_PATH="C:\\Program Files\\QGIS 3.28.9\\bin"

%OGR2OGR_PATH%\\ogr2ogr -f PostgreSQL -update -append PG:"host=%HOST% port=%PORT% dbname=%DB_NAME% user=%USER% ACTIVE_SCHEMA=jkr" -nln taajama -nlt MULTIPOLYGON -dialect SQLITE -sql "SELECT ""Geometry"" as geom, ""Urakkaraja"" as nimi, %POPULATION% as vaesto_lkm, ""fid"" as taajama_id, '%DATE_FROM%' as alkupvm FROM ""%SHP_TABLE%""" "%SHP_FILE%"
"%QGIS_BIN_PATH%\\ogr2ogr" -f PostgreSQL -update -append PG:"host=%JKR_DB_HOST% port=%JKR_DB_PORT% dbname=%JKR_DB% user=%JKR_USER% ACTIVE_SCHEMA=jkr" -nln taajama -nlt MULTIPOLYGON -dialect SQLITE -sql "SELECT ""Geometry"" as geom, ""Urakkaraja"" as nimi, %POPULATION% as vaesto_lkm, ""fid"" as taajama_id, '%DATE_FROM%' as alkupvm FROM ""%SHP_TABLE%""" "%SHP_FILE%"

ECHO Valmis!
Loading

0 comments on commit ed29143

Please sign in to comment.