-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnaip_import_aws.sh
executable file
·77 lines (66 loc) · 3.24 KB
/
naip_import_aws.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env bash
STATES=(al ar az ca co ct de fl ga ia id il in ks ky la ma md me mi mn mo ms mt nc nd ne nh nj nm nv ny oh ok or pa ri sc sd tn tx ut va vt wa wi wv wy)
END_YEAR=$(date +'%Y')
FAILING_SHAPEFILES=(naip_3_172_6_fl naip_3_17_2_8_sc naip_3_17_2_2_pa)
cd index
# TODO contact ESRI bucket owner about broken prj files
# Failures
# naip_3_13_2_1_al
# GEOGCS["GCS_North_American_1983",DATUM["D_North_American_1983",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]
docker rm -f naip-metadata-postgis
docker run -d --env POSTGRES_PASSWORD=cabbage \
--env POSTGRES_USER=user \
--env POSTGRES_DB=testdb \
-p 5432:5432 --name=naip-metadata-postgis mdillon/postgis
echo starting db
sleep 40
for i in *.shp
do
[[ -f "$i" ]] || break
bad_bananas=$(basename ${i} .shp)
# if equals naip_3_172_6_fl.shp skip
# TODO contact ESRI about broken shp file:
# naip_3_172_6_fl.shp
if [[ ! ${FAILING_SHAPEFILES[*]} =~ "${bad_bananas}" ]]
then
no_ext=${i%.*}
# # repair projection file
cat $no_ext".prj"
echo "overwrite pr file $no_ext.prj"
echo -e "GEOGCS[\"GCS_North_American_1983\",DATUM[\"D_North_American_1983\",SPHEROID[\"GRS_1980\",6378137.0,298.257222101]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]]" > $no_ext".prj"
# # # write sql
echo "write shapefile $1 to db"
ogr2ogr -f "PostgreSQL" PG:"host='localhost' port=5432 dbname='testdb' user='user' password='cabbage'" -append $i -nln naip_visual -t_srs EPSG:4326
# TODO contact ESRI about broken dbf files:
# naip_3_17_2_2_pa
# naip_3_17_2_8_sc
# look for shapefiles that are missing rows. if missing then sound an alarm
NULL_COUNT=$(docker exec -it naip-metadata-postgis /bin/bash -c "psql -qtA -d testdb -U user -c \"SELECT COUNT(*) FROM naip_visual WHERE res IS NULL\"")
echo null row count: $NULL_COUNT
else
echo skipping bad shapefile: $bad_bananas
fi
done
# TODO check that shapefile count matches the postgres row count
# add demical places to numeric field for resolution
# ALTER TABLE naip_visual
# ALTER COLUMN res
# SET DATA TYPE NUMERIC(10,2);
echo altering resolution column type
docker exec -it naip-metadata-postgis /bin/bash -c "psql -qtA -d testdb -U user -c \"ALTER TABLE naip_visual ALTER COLUMN res SET DATA TYPE NUMERIC(10,2);\""
# update all 0 values to be .6m
# UPDATE naip_visual
# SET res = 0.6
# WHERE res = 0;
echo altering resolution data from 0s to 0.6s
docker exec -it naip-metadata-postgis /bin/bash -c "psql -qtA -d testdb -U user -c \"UPDATE naip_visual SET res = 0.6 WHERE res = 0;\""
# fix all the date columns to correct type
# ALTER TABLE naip_visual
# ALTER COLUMN verdate TYPE DATE
# USING to_date(verdate, 'YYYYMMDD');
# ALTER TABLE naip_visual
# ALTER COLUMN srcimgdate TYPE DATE
# USING to_date(srcimgdate, 'YYYYMMDD');
echo altering dates from varchar to dates
docker exec -it naip-metadata-postgis /bin/bash -c "psql -qtA -d testdb -U user -c \"ALTER TABLE naip_visual ALTER COLUMN verdate TYPE DATE USING to_date(verdate, 'YYYYMMDD'); ALTER TABLE naip_visual ALTER COLUMN srcimgdate TYPE DATE USING to_date(srcimgdate, 'YYYYMMDD');\""
echo finished all tasks!