forked from NOC-OI/favourite-places
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csvtogeojson.sh
executable file
·63 lines (47 loc) · 1.67 KB
/
csvtogeojson.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
#get the number of lines we have
#wc -l outputs the number of lines and file name, use cut just to get the line count which is the first field
lines=$(wc -l $1 | cut -d' ' -f 1 )
echo "{ \"type\": \"FeatureCollection\", \"features\": [" > $2
for linenum in $(seq 2 $lines) ; do
line=$(head -n $linenum $1 | tail -1)
echo "Line: $linenum"
echo $line
name=$(echo $line | cut -d, -f 1)
if [ "$name" = "" ] ; then
echo "Missing name"
exit 1
fi
markersymbol=$(echo $line | cut -d, -f 2)
if [ "$markersymbol" = "" ] ; then
echo "Missing markersymbol"
exit 1
fi
creator=$(echo $line | cut -d, -f 3)
if [ "$creator" = "" ] ; then
echo "Missing creator"
exit 1
fi
comment=$(echo $line | cut -d, -f 4)
if [ "$comment" = "" ] ; then
echo "Missing comment"
exit 1
fi
lon=$(echo $line | cut -d, -f 5)
if [ "$lon" = "" ] ; then
echo "Missing lon"
exit 1
fi
lat=$(echo $line | cut -d, -f 6)
if [ "$lat" = "" ] ; then
echo "Missing lat"
exit 1
fi
echo "{ \"type\": \"Feature\", \"properties\": { \"marker-size\": \"medium\", \"marker-symbol\": \"$markersymbol\", \"name\": \"$name\", \"creator\" : \"$creator\", \"comment\" : \"$comment\" }," >> $2
#don't put a trailing comma on the last line
if [ "$linenum" = "$lines" ] ; then
echo "\"geometry\": { \"type\": \"Point\", \"coordinates\": [$lon,$lat] } }" >> $2
else
echo "\"geometry\": { \"type\": \"Point\", \"coordinates\": [$lon,$lat] } }," >> $2
fi
done
echo "] }" >> $2