-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate-map
More file actions
executable file
·410 lines (378 loc) · 12.4 KB
/
Copy pathgenerate-map
File metadata and controls
executable file
·410 lines (378 loc) · 12.4 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#!/usr/bin/env bash
# Generate a static map with 2-letter labels from CSV coordinates
# Usage: ./generate-map [OPTIONS] <csv_file>
set -euo pipefail
main() {
if [[ ! -f "$(dirname "$0")/common-functions" ]]; then
echo "Downloading common-functions from GitHub..."
if ! curl -fsSL https://raw.githubusercontent.com/Flower7C3/bash-tools/master/common-functions -o "$(dirname "$0")/common-functions"; then
echo "Failed to download common-functions"
exit 1
fi
fi
# shellcheck source=common-functions
source "$(dirname "$0")/common-functions"
generate_map_main "$@"
}
generate_map_main() {
show_usage() {
log_usage_title '[OPTIONS] <csv_file>'
echo
echo 'Build a GeoApify map with 2-letter marker labels from a CSV'
echo
log_header 'Options'
log_usage_options_line '-l;--label-col <NAME|N>' \
'CSV column used for 2-letter labels (required)'
log_usage_options_line '--lat-col <NAME|N>' \
'Latitude column (default: lat, latitude)'
log_usage_options_line '--lng-col <NAME|N>' \
'Longitude column (default: lng, lon, longitude)'
log_usage_options_line '-k;--api-key <KEY>' \
'GeoApify API key (or env GEOAPIFY_API_KEY)'
log_usage_options_line '-o;--output <FILE|DIR>' \
'Output image or directory (default: <csv-dir>/map.jpg)'
log_usage_options_line '-z;--zoom <ZOOM>' \
'Zoom (sets map center to the average of points)'
log_usage_options_line '-c;--center <LAT>,<LNG>' \
'Override map center'
log_usage_options_line '-w;--width <WIDTH>' \
'Image width (default: %s)' "$DEFAULT_WIDTH"
log_usage_options_line '--height <HEIGHT>' \
'Image height (default: %s)' "$DEFAULT_HEIGHT"
log_usage_options_line '--color <COLOR>' \
'Marker color (default: %s)' "$DEFAULT_COLOR"
log_usage_options_line '--color-col <NAME|N>' \
'Optional CSV column with per-point colors'
log_usage_options_line '-t;--marker-type <TYPE>' \
'circle, awesome, material, plain (default: %s)' "$DEFAULT_MARKER_TYPE"
log_usage_options_line '--type-col <NAME|N>' \
'Optional CSV column with per-point marker type'
log_usage_options_line '--marker-size <PX>' \
'Marker size (default: %s)' "$DEFAULT_MARKER_SIZE"
log_usage_options_line '-v;--verbose' \
'Verbose output'
log_usage_options_line '-d;--debug' \
'Debug output'
log_usage_options_line '-u;--update' \
'Update app'
log_usage_options_line '-h;--help' \
'Show this help message'
echo
log_header 'CSV'
echo ' Header row required. Label values are trimmed to 2 letters (A-Z, 0-9).'
echo ' Optional type column: circle, awesome, material, plain.'
echo ' Geocode first when the file has addresses only:'
echo " $(styler text YELLOW)./geocode-addresses -a adres places.csv$(styler reset)"
echo
log_header 'Examples'
log_usage_example_line '--label-col kod -k YOUR_KEY places-geocoded.csv'
log_usage_example_line '-l initials -t awesome --type-col kind data.csv'
log_usage_example_line '-l 1 --lat-col lat --lng-col lng -o ~/Pictures/trasy.jpg geo.csv'
exit 0
}
local DEFAULT_WIDTH=1200 DEFAULT_HEIGHT=800 DEFAULT_COLOR="#e0222d"
local DEFAULT_MARKER_TYPE="circle" DEFAULT_MARKER_SIZE=48
local LABEL_COL="" LAT_COL="" LNG_COL="" API_KEY="${GEOAPIFY_API_KEY:-}"
local OUTPUT="" ZOOM="" CENTER="" WIDTH="$DEFAULT_WIDTH" HEIGHT="$DEFAULT_HEIGHT"
local COLOR="$DEFAULT_COLOR" COLOR_COL="" MARKER_TYPE="$DEFAULT_MARKER_TYPE" TYPE_COL=""
local MARKER_SIZE="$DEFAULT_MARKER_SIZE" CSV_FILE="" VERBOSE=0
is_marker_type() {
case "$1" in
circle | awesome | material | plain) return 0 ;;
*) return 1 ;;
esac
}
# shellcheck disable=SC2034
local DEBUG=0
parse_arguments() {
while [[ $# -gt 0 ]]; do
case "$1" in
-l | --label-col)
LABEL_COL="$2"
shift 2
;;
--lat-col)
LAT_COL="$2"
shift 2
;;
--lng-col)
LNG_COL="$2"
shift 2
;;
-k | --api-key)
API_KEY="$2"
shift 2
;;
-o | --output)
OUTPUT="$2"
shift 2
;;
-z | --zoom)
ZOOM="$2"
if ! [[ "$ZOOM" =~ ^[0-9]+$ ]] || [[ "$ZOOM" -lt 0 ]] || [[ "$ZOOM" -gt 20 ]]; then
die 1 'Invalid zoom level: %s. Must be between 0 and 20' "$ZOOM"
fi
shift 2
;;
-c | --center)
CENTER="$2"
shift 2
;;
-w | --width)
WIDTH="$2"
shift 2
;;
--height)
HEIGHT="$2"
shift 2
;;
--color)
COLOR="$2"
shift 2
;;
--color-col)
COLOR_COL="$2"
shift 2
;;
-t | --marker-type)
MARKER_TYPE="$2"
if ! is_marker_type "$MARKER_TYPE"; then
die 1 'Invalid marker type: %s. Use circle, awesome, material, plain' "$MARKER_TYPE"
fi
shift 2
;;
--type-col)
TYPE_COL="$2"
shift 2
;;
--marker-size)
MARKER_SIZE="$2"
shift 2
;;
-v | --verbose)
VERBOSE=1
shift
;;
-d | --debug)
DEBUG=1
shift
;;
-u | --update)
update_application
;;
-h | --help)
show_usage
;;
-*)
log_error 'Unknown option: <b>%s</b>' "$1"
echo
show_usage
;;
*)
if [[ -n "$CSV_FILE" ]]; then
die 1 'Unexpected argument: <b>%s</b>' "$1"
fi
CSV_FILE="$1"
shift
;;
esac
done
}
parse_arguments "$@"
if [[ -z "$CSV_FILE" ]]; then
log_error 'CSV file is required'
echo
show_usage
fi
if [[ ! -f "$CSV_FILE" ]]; then
die 1 'CSV file not found: <u>%s</u>' "$CSV_FILE"
fi
if [[ -z "$LABEL_COL" ]]; then
die 1 'Label column is required. Use <code>--label-col</code>'
fi
if [[ -z "$API_KEY" ]]; then
die 1 'API key is required. Use <code>--api-key</code> or <code>GEOAPIFY_API_KEY</code>'
fi
local DOWNLOAD_MAP
DOWNLOAD_MAP="$(cd "$(dirname "$0")" && pwd)/download-map"
if [[ ! -x "$DOWNLOAD_MAP" ]]; then
die 1 'download-map not found next to this script: <u>%s</u>' "$DOWNLOAD_MAP"
fi
check_dependencies python3 curl
log_title 'Generate Map'
local POINTS _py_status=0 AVG_LAT="" AVG_LNG=""
POINTS="$(
LABEL_COL="$LABEL_COL" LAT_COL="$LAT_COL" LNG_COL="$LNG_COL" COLOR_COL="$COLOR_COL" \
TYPE_COL="$TYPE_COL" \
python3 - "$CSV_FILE" <<'PY'
import csv
import io
import os
import re
import sys
path = sys.argv[1]
label_spec = os.environ["LABEL_COL"]
lat_spec = os.environ.get("LAT_COL", "")
lng_spec = os.environ.get("LNG_COL", "")
color_spec = os.environ.get("COLOR_COL", "")
type_spec = os.environ.get("TYPE_COL", "")
raw = open(path, encoding="utf-8-sig", newline="").read()
sample = raw[:4096]
try:
dialect = csv.Sniffer().sniff(sample, delimiters=",;\t|")
except csv.Error:
dialect = csv.excel
reader = csv.DictReader(io.StringIO(raw), dialect=dialect)
names = list(reader.fieldnames or [])
if not names:
raise SystemExit("CSV has no header")
def resolve(spec, fallbacks):
if spec:
if spec.isdigit():
idx = int(spec) - 1
if idx < 0 or idx >= len(names):
raise SystemExit(f"Column index out of range: {spec}")
return names[idx]
for name in names:
if name == spec or name.lower() == spec.lower():
return name
raise SystemExit(f"Column not found: {spec}")
lowered = {name.lower(): name for name in names}
for cand in fallbacks:
if cand in lowered:
return lowered[cand]
return None
label_col = resolve(label_spec, ())
lat_col = resolve(lat_spec, ("lat", "latitude", "szerokosc", "y"))
lng_col = resolve(lng_spec, ("lng", "lon", "long", "longitude", "dlugosc", "x"))
color_col = resolve(color_spec, ()) if color_spec else None
type_col = resolve(type_spec, ()) if type_spec else None
if not label_col:
raise SystemExit("Label column is required")
if not lat_col or not lng_col:
raise SystemExit("Latitude/longitude columns not found")
print(f"#cols\t{label_col}\t{lat_col}\t{lng_col}")
points = []
for i, row in enumerate(reader, 2):
raw_label = (row.get(label_col) or "").strip()
letters = re.sub(r"[^0-9A-Za-zĄĆĘŁŃÓŚŹŻąćęłńóśźż]", "", raw_label)
label = letters[:2].upper()
lat = (row.get(lat_col) or "").strip().replace(",", ".")
lng = (row.get(lng_col) or "").strip().replace(",", ".")
color = (row.get(color_col) or "").strip() if color_col else ""
mtype = (row.get(type_col) or "").strip().lower() if type_col else ""
if not label:
print(f"#skip\tline {i}: empty label")
continue
if not lat or not lng:
print(f"#skip\tline {i}: missing coordinates for {label}")
continue
points.append((label, lat, lng, color, mtype))
print(f"{label}\t{lat}\t{lng}\t{color}\t{mtype}")
if points:
avg_lat = sum(float(p[1]) for p in points) / len(points)
avg_lng = sum(float(p[2]) for p in points) / len(points)
print(f"#meta\t{len(points)}\t{avg_lat:.6f}\t{avg_lng:.6f}")
PY
)" || _py_status=$?
if [[ "$_py_status" -ne 0 ]]; then
die 1 'Failed to read CSV'
fi
local MARKERS="" COUNT=0
local label lat lng color_value type_value content_size
content_size=$((MARKER_SIZE / 2))
[[ "$content_size" -lt 14 ]] && content_size=14
[[ "$content_size" -gt 22 ]] && content_size=22
while IFS=$'\t' read -r label lat lng color_value type_value; do
[[ -z "$label" ]] && continue
if [[ "$label" == "#cols" ]]; then
log_debug 'Columns: label=<b>%s</b> lat=<b>%s</b> lng=<b>%s</b>' "$lat" "$lng" "$color_value"
continue
fi
if [[ "$label" == "#skip" ]]; then
log_warning '%s' "$lat${lng:+ $lng}${color_value:+ $color_value}"
continue
fi
if [[ "$label" == "#meta" ]]; then
COUNT="$lat"
AVG_LAT="$lng"
AVG_LNG="$color_value"
continue
fi
if [[ -z "$color_value" ]]; then
color_value="$COLOR"
fi
if [[ -z "$type_value" ]]; then
type_value="$MARKER_TYPE"
fi
if ! is_marker_type "$type_value"; then
log_warning 'Row %s: unknown type <b>%s</b>, using %s' "$label" "$type_value" "$MARKER_TYPE"
type_value="$MARKER_TYPE"
fi
local marker="lonlat:${lng},${lat};type:${type_value};color:${color_value};size:${MARKER_SIZE};text:${label};contentsize:${content_size};contentcolor:white;whitecircle:no"
if [[ -z "$MARKERS" ]]; then
MARKERS="$marker"
else
MARKERS="${MARKERS}|${marker}"
fi
if [[ "$VERBOSE" == "1" ]]; then
log_info ---icon '📍' '%s %s,%s' "$label" "$lat" "$lng"
fi
log_debug 'Marker: %s' "$marker"
done <<<"$POINTS"
if [[ -z "$MARKERS" ]]; then
die 1 'No valid points with labels and coordinates'
fi
local OUT_DIR OUT_FILE TMP_DIR
if [[ -z "$OUTPUT" ]]; then
OUT_DIR="$(cd "$(dirname "$CSV_FILE")" && pwd)"
OUT_FILE="$OUT_DIR/map.jpg"
elif [[ -d "$OUTPUT" ]]; then
OUT_DIR="$(cd "$OUTPUT" && pwd)"
OUT_FILE="$OUT_DIR/map.jpg"
else
mkdir -p "$(dirname "$OUTPUT")"
OUT_DIR="$(cd "$(dirname "$OUTPUT")" && pwd)"
OUT_FILE="$OUT_DIR/$(basename "$OUTPUT")"
fi
TMP_DIR="$(mktemp -d "${TMPDIR:-/tmp}/generate-map.XXXXXX")"
trap 'rm -rf "$TMP_DIR"' EXIT
local -a MAP_ARGS=(
--provider geoapify
--api-key "$API_KEY"
--width "$WIDTH"
--height "$HEIGHT"
--output "$TMP_DIR"
--markers "$MARKERS"
)
[[ "$VERBOSE" == "1" ]] && MAP_ARGS+=(--verbose)
[[ "${DEBUG:-0}" == "1" ]] && MAP_ARGS+=(--debug)
if [[ -n "$CENTER" ]]; then
MAP_ARGS+=(--center "$CENTER")
MAP_ARGS+=(--zoom "${ZOOM:-15}")
elif [[ -n "$ZOOM" ]]; then
MAP_ARGS+=(--center "${AVG_LAT},${AVG_LNG}")
MAP_ARGS+=(--zoom "$ZOOM")
log_debug 'Center: %s,%s zoom %s' "$AVG_LAT" "$AVG_LNG" "$ZOOM"
else
log_debug 'Auto-fitting %s markers' "$COUNT"
fi
log_info ---icon '📍' 'Markers: <b>%s</b>' "$COUNT"
log_info ---icon '🗺' 'Calling download-map...'
"$DOWNLOAD_MAP" "${MAP_ARGS[@]}"
local downloaded=""
local -a _imgs=()
shopt -s nullglob
_imgs=("$TMP_DIR"/*.jpg "$TMP_DIR"/*.png)
shopt -u nullglob
downloaded="${_imgs[0]:-}"
if [[ -z "$downloaded" || ! -f "$downloaded" ]]; then
die 1 'download-map did not produce an image'
fi
mkdir -p "$OUT_DIR"
mv "$downloaded" "$OUT_FILE"
rm -rf "$TMP_DIR"
trap - EXIT
log_success ---icon '🗺' 'Map saved: <u>%s</u>' "$OUT_FILE"
}
main "$@"