-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgeoplot.ado
4076 lines (3909 loc) · 132 KB
/
geoplot.ado
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
*! version 1.3.5 17sep2024 Ben Jann
capt which colorpalette
if _rc==1 exit _rc
local rc_colorpalette = _rc
capt findfile lcolrspace.mlib
if _rc==1 exit _rc
local rc_colrspace = _rc
capt mata: assert(mm_version()>=200)
if _rc==1 exit _rc
local rc_moremata = _rc
if `rc_colorpalette' | `rc_colrspace' | `rc_moremata' {
if `rc_colorpalette' {
di as err "{bf:colorpalette} is required; " _c
di as err "type {stata ssc install palettes, replace}"
}
if `rc_colrspace' {
di as err "{bf:colrspace} is required; " _c
di as err "type {stata ssc install colrspace, replace}"
}
if `rc_moremata' {
di as err "{bf:moremata} version 2.0.0 or newer is required; " _c
di as err "type {stata ssc install moremata, replace}"
}
exit 499
}
program geoplot
version 16.1
nobreak {
capture noisily break {
mata: _GEOPLOT_ColrSpace_S = ColrSpace() // add global object
_geoplot `0'
}
local rc = _rc
capt mata mata drop _GEOPLOT_ColrSpace_S // remove global object
exit `rc'
}
end
program _geoplot, rclass
// parse layers
_parse expand layer lg : 0
if `"`lg_if'"'!="" {
di as err "global {bf:if} not allowed"
exit 198
}
if `"`lg_in'"'!="" {
di as err "global {bf:in} not allowed"
exit 198
}
local 0 , `lg_op'
// parse global options
syntax [, /*
*/ NOLEGend LEGend LEGend2(str asis) CLEGend CLEGend2(str asis)/*
*/ SBAR SBAR2(str asis) COMPass COMPass2(str asis)/*
*/ PROJect PROJect2(str) ANGle(real 0) rotate(real 0)/*
*/ BACKground BACKground2(str) tissot TISSOT2(str)/*
*/ tight Margin(str) REFdim(str) ASPECTratio(str) PCYCle(passthru)/*
*/ YSIZe(passthru) XSIZe(passthru) axes SCHeme(passthru) /*
*/ frame(str) NOGRAPH NOISily * ]
if `"`project2'"'!="" local project project
if "`project'"!="" _parse_project `project2'
if `"`background2'"'!="" local background background
if "`background'"!="" _parse_background `project', `background2'
_collect_repeated GRID2 `"`macval(options)'"' // grid_n, grid_1 ...
if `"`tissot2'"'!="" local tissot tissot
if "`tissot'"!="" _parse_tissot, `tissot2'
if !`angle' local angle `rotate'
_parse_aspectratio `aspectratio' // returns ar, ar_opts
_parse_refdim `refdim'
_parse_frame `frame' // returns frame, replace, nocurrent
_collect_repeated zoom `"`macval(options)'"' // zoom_n, zoom_1 ...
_collect_repeated inset `"`macval(options)'"' // inset_n, inset_1 ...
_collect_repeated GLEGend2 `"`macval(options)'"' // glegend_n ...
_collect_repeated SLEGend `"`macval(options)'"' // slegend_n ...
local legend = `"`legend'`legend2'"'!=""
local clegend = `"`clegend'`clegend2'"'!=""
if !`legend' & !`glegend_n' & !`clegend' & "`nolegend'"=="" local legend 1
local options `pcycle' `options'
// prepare frame
local cframe = c(frame)
tempname main
frame create `main'
frame `main' {
/* default variables:
LAYER: layer ID
Y: Y coordinate
X: X coordinate
W: weight
further variables possibly generated along the way:
PLV plot level for enclaves and exclaves
Z: categorized zvar()
Y2: secondary Y coordinate (pc)
X2: secondary X coordinate (pc)
MLAB: marker labels
MLABPOS: marker label positions
helper variables that will be dropped at the end
cY cX: centroids for non-rotating objects
*/
gen byte LAYER = .
char LAYER[layers] `layer_n'
gen double Y = .
gen double X = .
qui set obs 2
gen double W = _n - 1 // 0 and 1
// background: generate plot with empty coordinates
local p 0
local p0 0
local plots
if "`background'"!="" {
_background `p' `bg_n' `"`bg_feat'"' `"`background2'"' // plot, p
local p0 `p'
local plots `plots' `plot'
}
// process insets with option below
local N0 = _N
forv i = 1/`inset_n' {
local n0 = _N
capt n _inset 1 `"`project2'"' `"`project_opts'"' `angle'/*
*/ `i' `p' "`cframe'", `inset_`i'' /* plot, p, ins_i,
ins_i_ti, ins_i_s, ins_i_pos, ins_i_mrg, ins_i_xm, ins_i_ym */
if _rc==1 exit 1
if _rc {
di as err "error in inset()"
exit _rc
}
if `ins_`i'' {
local ins_`i'_n0 = `n0' + 1
local ins_`i'_n1 = _N
local p0 `p'
local plots `plots' `plot'
}
}
local N1 = _N
if `N1'>`N0' {
tempname insframe
frame copy `main' `insframe'
// replace inset data by missing
qui keep in 1/`N0'
qui set obs `N1'
}
// process layers
forv i = 1/`layer_n' {
// provide tempnames for returns (matrices)
if `"`: char LAYER[CUTS]'"'=="" {
tempname TMP
char LAYER[CUTS] `TMP'
}
if `"`: char LAYER[NOBS]'"'=="" {
tempname TMP
char LAYER[NOBS] `TMP'
}
// parse plottype and frame
_parse_layer "`cframe'" `layer_`i'' // plottype, lframe, layer
// generate plot
capt n _geoplot_`plottype' `i' `p' `lframe' `layer' // plot, p
if _rc==1 exit 1
if _rc {
di as err "error in layer `i': `plottype' ..."
exit _rc
}
local plots `plots' `plot'
}
char LAYER[CUTS]
char LAYER[NOBS]
if `p'<=`p0' {
return scalar layers = 0
char LAYER[layers]
di as txt "(no layers created; nothing to plot)"
exit
}
// background: fill in coordinates
local N1 = _N
if "`background'"!="" {
_background_fillin `N1' 4 `bg_n' "`bg_pad'" `bg_limits'
}
// grid lines and Tissot's indicatrices
local N1 = _N
local grid_lbls 0
forv i = 1/`grid_n' {
capt n _grid `p', `grid_`i'' // plot, p, grid_lbls
if _rc==1 exit 1
if _rc {
di as err "error in grid()"
exit _rc
}
local plots `plots' `plot'
}
local N2 = _N
if "`tissot'"!="" {
_tissot `N1' `p' `"`tissot2'"' `"`tissot_opts'"' `"`tissot_mark'"'
local plots `plots' `plot'
}
if `grid_n' & "`background'"!="" {
// update background to cover grid (unless all limits are custom
// or padding contains negative values)
capt numlist "`bg_pad'", range(>=0)
if _rc==1 exit 1
if !_rc {
foreach l of local bg_limits {
if `l'<. continue
_background_fillin `N2' 4 `bg_n' "0 0 0 0" `bg_limits'
continue, break
}
}
}
// project map
_project `"`project2'"' `"`project_opts'"'
local xrescale = r(xrescale)
// rotate map
_rotate `angle'
capt drop cY cX
// zoom
local N1 = _N
forv i = 1/`zoom_n' {
capt n _zoom `N1' "`refdim'" `p' `layer_n', `zoom_`i'' // plot, p
if _rc==1 exit 1
if _rc {
di as err "error in zoom()"
exit _rc
}
local plots `plots' `plot'
}
// axes option
_parse_axes, `axes' `options'
// graph dimensions
if `"`margin'"'=="" & `grid_lbls' local margin 10 10 5 5
else mata: _geo_parse_marginexp("margin", `"`margin'"', 0, 0)
_grdim, margin(`margin') refdim(`refdim') aratio(`ar') `options'
// refsize Ymin Ymax Xmin Xmax ar ar_units yxratio options
local ar_opts `ar_units' `ar_opts'
if `"`ar_opts'"'!="" local ar_opts `", `ar_opts'"'
local aspectratio aspectratio(`ar'`ar_opts')
if "`tight'"!="" { // update ysize and ysize
_grdim_tight, aratio(`yxratio') `scheme' `ysize' `xsize'
}
// insets
forv i = 1/`inset_n' {
// collect data from inset with option below
if `ins_`i'' {
mata: _inset_get("`insframe'", `ins_`i'_n0', `ins_`i'_n1')
}
// process remaining inset
else {
local n0 = _N
capt n _inset 0 `"`project2'"' `"`project_opts'"' `angle'/*
*/ `i' `p' "`cframe'", `inset_`i'' /* plot, p, ins_i,
ins_i_ti, ins_i_s, ins_i_pos, ins_i_mrg, ins_i_xm, ins_i_ym */
if _rc==1 exit 1
if _rc {
di as err "error in inset()"
exit _rc
}
local ins_`i'_n0 = `n0' + 1
local ins_`i'_n1 = _N
local plots `plots' `plot'
}
// set size and position of inset
mata: _inset(`ins_`i'_n0', `ins_`i'_n1', `ins_`i'_s',/*
*/ `ins_`i'_pos', strtoreal(tokens("`ins_`i'_mrg'")),/*
*/ `ins_`i'_ym', `ins_`i'_xm', "`refdim'", `refsize',/*
*/ `Ymin', `Ymax', `Xmin', `Xmax')
// add inset title(s)
if `"`ins_`i'_ti'"'!="" {
_inset_add_title `p' `ins_`i'_n0' `ins_`i'_n1' `ins_`i'_ti'
local plots `plots' `plot'
}
}
// scale bar
if `"`sbar'`sbar2'"'!="" {
_scalebar `refsize' `Ymin' `Ymax' `Xmin' `Xmax', `sbar2'
local plots `plots' `plot'
}
// compass
if `"`compass'`compass2'"'!="" {
_compass `refsize' `Ymin' `Ymax' `Xmin' `Xmax', `compass2'
local plots `plots' `plot'
}
// legend
if `legend' {
_legend, `legend2' // returns legend, legend_pos
}
else local legend legend(off)
// clegend
if `clegend' {
_clegend `legend_pos', `clegend2' _gropts(`options') // plot clegend
local plots `plots' `plot'
}
else local clegend
// glegend
forv i = 1/`glegend_n' {
capt n _Legend glegend "`refdim'" `refsize'/*
*/ `Ymin' `Ymax' `Xmin' `Xmax', `pcycle' `glegend_`i'' // plot
if _rc==1 exit 1
if _rc {
di as err "error in glegend()"
exit _rc
}
local plots `plots' `plot'
}
// slegend
forv i = 1/`slegend_n' {
capt n _Legend slegend "`refdim'" `refsize' `Ymin' `Ymax'/*
*/ `Xmin' `Xmax', xrescale(`xrescale') `slegend_`i'' // plot
if _rc==1 exit 1
if _rc {
di as err "error in slegend()"
exit _rc
}
local plots `plots' `plot'
}
// move char to r()
forv i = `layer_n'(-1)1 {
local lchars
local schars
local mchars
if `"`: char LAYER[hasz_`i']'"'=="1" {
local schars `schars' z_discrete
local lchars `lchars' z_colors
local schars `schars' z_hascol z_hasmis
local mchars `mchars' z_nobs z_levels
}
local schars `schars' hasz
local lchars `lchars' labels keys
foreach char of local lchars {
return local `char'_`i' `"`:char LAYER[`char'_`i']'"'
char LAYER[`char'_`i']
}
foreach char of local schars {
return scalar `char'_`i' = `:char LAYER[`char'_`i']'
char LAYER[`char'_`i']
}
foreach char of local mchars {
local TMP: char LAYER[`char'_`i']
char LAYER[`char'_`i']
return matrix `char'_`i' = `TMP'
}
char LAYER[z_reverse_`i']
char LAYER[z_mleg_`i']
char LAYER[z_format_`i']
char LAYER[nolegend_`i']
}
return scalar layers = `layer_n'
char LAYER[layers]
// draw graph
local graph /*
*/ graph twoway `plots', `legend' `clegend' /*
*/ `scheme' `aspectratio' `ysize' `xsize' `options'
if "`nograph'"=="" {
`graph'
}
}
// display graph command
if "`noisily'"!="" {
_di_grcmd `graph'
}
// returns
return local legend `legend'
return local graph `graph'
if "`frame'"!="" {
local cframe = c(frame)
capt confirm new frame `frame'
if _rc==1 exit 1
if _rc {
if "`frame'"==`"`cframe'"' { // cannot drop current frame
frame change `main'
local cframe `frame'
}
frame drop `frame'
}
frame rename `main' `frame'
di as txt "(graph data stored as frame {bf:`frame'})"
if "`nocurrent'"=="" {
if "`frame'"!=`"`cframe'"' {
frame change `frame'
di as txt "(current frame now {bf:`frame'})"
}
}
}
end
program _parse_layer
gettoken cframe layer : 0
gettoken plottype layer : layer, parse(" ,")
_parse_plottype `plottype' // returns plottype, isimmediate
if `isimmediate' local lframe
else {
gettoken lframe : layer, parse(" ,[")
if `"`lframe'"'=="" {
// frame may have been specified as ""
gettoken lframe layer : layer, parse(" ,[")
local lframe `"`cframe'"'
}
else if inlist(`"`lframe'"', ",", "if", "in", "[") {
// leave layer as is
local lframe `"`cframe'"'
}
else {
// remove frame from layer
gettoken lframe layer : layer, parse(" ,[")
if `"`lframe'"'=="." local lframe `"`cframe'"'
}
}
c_local plottype `"`plottype'"'
c_local lframe `"`lframe'"'
c_local layer `"`layer'"'
end
program _parse_plottype
local isimmediate 0
local l = strlen(`"`0'"')
if `"`0'"'==substr("areas", 1, max(4,`l')) local 0 area
else if `"`0'"'==substr("lines", 1, max(4,`l')) local 0 line
else if `"`0'"'==substr("points", 1, max(5,`l')) local 0 point
else if `"`0'"'==substr("scatter", 1, max(2,`l')) local 0 scatter
else if `"`0'"'==substr("labels", 1, max(3,`l')) local 0 label
else if `"`0'"'==substr("symbols", 1, max(3,`l')) local 0 symbol
else if `"`0'"'==substr("pies", 1, max(3,`l')) local 0 pie
else if `"`0'"'==substr("bars", 1, max(3,`l')) local 0 bar
else {
capt mata: assert(st_islmname(st_local("0")))
if _rc==1 exit _rc
if _rc {
di as err `"`0' invalid plottype"'
exit 198
}
if substr("`0'",-1,1)=="i" local isimmediate 1 /* this prevents geoplot
from adding a frame to the layer specification */
}
c_local plottype `"`0'"'
c_local isimmediate `isimmediate'
end
program _collect_repeated
args OPT options
local opt = strlower("`OPT'")
local i 0
// collect OPT without arguments, if relevant
if substr("`opt'",-1,.)=="2" {
local OPT0 = substr("`OPT'", 1, strlen("`OPT'")-1)
local opt0 = substr("`opt'", 1, strlen("`opt'")-1)
local 0 , `macval(options)'
syntax [, `OPT0' * ]
if "``opt0''"!="" {
local ++i
c_local `opt0'_`i'
}
}
else local opt0 `opt'
// collect instances of OPT()
while (1) {
local 0 , `macval(options)'
syntax [, `OPT'(passthru) * ]
if `"``opt''"'=="" continue, break
local ++i
c_local `opt0'_`i' ``opt''
}
c_local `opt0'_n `i'
c_local options: copy local options
end
program _parse_aspectratio
_parse comma lhs 0 : 0
if `"`lhs'"'!="" {
numlist "`lhs'", max(1)
local lhs `r(numlist)'
}
else local lhs 1 // default
syntax [, UNITs * ]
c_local ar `lhs'
c_local ar_opts `options'
end
program _parse_refdim
if `"`0'"'=="" exit
local l = strlen(`"`0'"')
if `"`0'"'=="y" local 0 y
else if `"`0'"'=="x" local 0 x
else if `"`0'"'==substr("vertical",1,`l') local 0 y
else if `"`0'"'==substr("horizontal",1,`l') local 0 x
else {
di as err "invalid specification in refdim()"
exit 198
}
c_local refdim `0'
end
program _parse_frame
syntax [name(name=frame)] [, replace NOCURrent ]
if "`frame'"=="" exit
if "`replace'"=="" {
qui _frame dir
local framelist = r(contents)
if `:list frame in framelist' {
di as err "frame {bf:`frame'} already exists"
exit 499
}
}
c_local frame `frame'
c_local replace `replace'
c_local nocurrent `nocurrent'
end
program _parse_background
_parse comma project 0 : 0
syntax [, Limits(numlist max=4 missingok) n(numlist max=1 >0)/*
*/ PADding(str) water * ]
if "`n'"=="" {
if "`project'"!="" local n 100
else local n 1
}
while (`: list sizeof limits'<4) {
local limits `limits' .
}
mata: _geo_parse_marginexp("padding", "`padding'")
c_local bg_limits `limits'
c_local bg_pad `padding'
c_local bg_n `n'
c_local bg_feat `water'
c_local background2 `options'
end
program _background
args p n feat opts
tempname background
frame create `background' byte(_ID) double(_X _Y)
frame `background' {
qui set obs `=`n'*4 + 2'
qui replace _ID = 1
if `"`feat'"'!="" geoframe set feature `feat'
}
_geoplot_area . `p' `background', `opts'
c_local plot `plot'
c_local p `p'
end
program _background_fillin
args N a n pad Xmin Xmax Ymin Ymax
foreach x in X Y {
gettoken lpad pad : pad
gettoken rpad pad : pad
if ``x'min'>=. | ``x'max'>=. {
_get_minmax `x' in 1/`N'
if ``x'min'>=. local `x'min = r(min) - (r(max)-r(min))*(`lpad'/100)
if ``x'max'>=. local `x'max = r(max) + (r(max)-r(min))*(`rpad'/100)
}
}
local b = `a' + `n'
mata: st_store((`a',`b'), "X", rangen(`Xmin',`Xmax',`n'+1))
mata: st_store((`a',`b'), "Y", J(`n'+1,1,`Ymin'))
local a = `b'
local b = `a' + `n'
mata: st_store((`a',`b'), "X", J(`n'+1,1,`Xmax'))
mata: st_store((`a',`b'), "Y", rangen(`Ymin',`Ymax',`n'+1))
local a = `b'
local b = `a' + `n'
mata: st_store((`a',`b'), "X", rangen(`Xmax',`Xmin',`n'+1))
mata: st_store((`a',`b'), "Y", J(`n'+1,1,`Ymax'))
local a = `b'
local b = `a' + `n'
mata: st_store((`a',`b'), "X", J(`n'+1,1,`Xmin'))
mata: st_store((`a',`b'), "Y", rangen(`Ymax',`Ymin',`n'+1))
end
program _grid
gettoken p 0 : 0, parse(" ,")
syntax [, GRID2(str) ]
local 0 `", `grid2'"'
syntax [, x(passthru) y(passthru) tight PADding(passthru) RADian/*
*/ n(passthru) noEXtend mesh LABels LABels2(str) * ]
if `"`labels2'"'!="" local labels labels
if "`labels'"!="" {
c_local grid_lbls 1 // update grid_lbls
_grid_parse_lbls, `labels2' // => labels, labels2
}
tempname GRID GRID_shp
capt confirm var X2, exact
if _rc==1 exit 1
if _rc local XY X Y
else local XY X Y X2 Y2
qui geoframe grid `GRID' `GRID_shp', coordinates(`XY')/*
*/ `x' `y' `tight' `padding' `radian' `n' `extend' `mesh'
_geoplot_line . `p' `GRID', `options'
local plots `plot'
if "`labels'"!="" {
frame `GRID' {
qui generate double X = cond(axis==2,_CY,_CX)
local 0 `", `labels2'"'
syntax [, Format(str) * ]
if `"`format'"'=="" local format: format X
capt confirm format `format'
if _rc==1 exit 1
if _rc==0 {
// format(%fmt)
capt confirm string format `format'
if _rc==1 exit 1
if _rc local format string(X, "`format'")
else local format string(X) // string format specified
}
else {
// format(exp)
local 0 `", `labels2'"'
syntax [, Format(str asis) * ]
}
generate str _LAB = `format'
}
}
while ("`labels'"!="") {
gettoken m labels : labels
gettoken pos labels : labels
if "`m'"=="l" local tmp if axis==2, coord(xmin ymin)
else if "`m'"=="r" local tmp if axis==2, coord(xmax ymin)
else if "`m'"=="b" local tmp if axis==1, coord(xmin ymin)
else /*m=t*/ local tmp if axis==1, coord(xmin ymax)
_geoplot_label . `p' `GRID' _LAB `tmp' position(`pos') `options'
local plots `plots' `plot'
}
c_local plot `plots'
c_local p `p'
end
program _grid_parse_lbls
syntax [, Positions(str) COLor(passthru) * ]
if `"`color'"'=="" local color color(gray)
capt n _grid_parse_labels `positions' // => labels
if _rc==1 exit 1
if _rc {
di as err "error in grid(labels(positions()))"
exit _rc
}
c_local labels `labels'
c_local labels2 `color' `options'
end
program _grid_parse_labels
while ("`0'"!="") {
gettoken m 0 : 0, parse("= ")
if !inlist(`"`m'"',"l","r","b","t") {
di as err `"{bf:`m'} not allowed"'
exit 198
}
gettoken eq : 0, parse("= ")
if `"`eq'"'=="=" gettoken eq 0 : 0, parse("= ") // remove "="
gettoken tok : 0 // check next token
if `"`eq'"'=="=" confirm number `tok'
capt numlist `"`tok'"', min(1) max(1) int range(>=0 <=12)
if _rc==1 exit 1
if _rc==0 { // valid clockpos specified
local pos `r(numlist)'
gettoken tok 0 : 0 // remove token
}
else {
if "`m'"=="l" local pos 9
else if "`m'"=="r" local pos 3
else if "`m'"=="b" local pos 6
else /*m=t*/ local pos 12
}
local labels `labels' `m' `pos'
}
if "`labels'"=="" local labels l 9 b 6
c_local labels `labels'
end
program _parse_tissot
syntax [, r(passthru) x(passthru) y(passthru) tight PADding(passthru) /*
*/ RADian n(passthru) MARKers MARKers2(str) * ]
if `"`markers2'"'=="" & "`markers'"!="" local markers2 " "
c_local tissot2 `r' `x' `y' `tight' `padding' `radian' `n'
c_local tissot_opts `options'
c_local tissot_mark `"`markers2'"'
end
program _tissot
args Nlast p tissot2 opts mark
tempname tissot tissot_shp
capt confirm var X2, exact
if _rc==1 exit 1
if _rc local XY X Y
else local XY X Y X2 Y2
qui geoframe tissot `tissot' `tissot_shp' in 1/`Nlast', coordinates(`XY')/*
*/ `tissot2'
_geoplot_area . `p' `tissot', `opts'
local plots `plot'
if `"`mark'"'!="" {
_geoplot_point . `p' `tissot', `mark'
local plots `plots' `plot'
}
c_local plot `plots'
c_local p `p'
end
program _parse_project
_parse comma project 0 : 0
syntax [, RADian ]
if `"`project'"'=="" local project webmercator
else {
gettoken pname : project
mata: (void) _geo_project_find(`"`pname'"')
}
c_local project2 `"`project'"'
c_local project_opts `radian'
end
program _project, rclass
args project opts in
tempname xrescale
scalar `xrescale' = 1
if strtrim(`"`project'"')=="" {
return scalar xrescale = `xrescale'
exit
}
capt confirm var X2, exact
if _rc==1 exit 1
if _rc local XY X Y
else local XY X Y X2 Y2
capt confirm var cX, exact
if _rc==1 exit 1
if _rc==0 { // lock non-rotating shapes
local XY `XY' cX cY
tempvar dY dX
qui gen double `dY' = Y - cY if cY<. `in'
qui gen double `dX' = X - cX if cX<. `in'
qui replace Y = cY if cY<. `in'
qui replace X = cX if cX<. `in'
su X `in', meanonly
scalar `xrescale' = r(max) - r(min)
}
geoframe project `project' `in', `opts' xy(`XY') fast noshp
if "`dY'"!="" { // rescale and restore non-rotating shapes
su X `in', meanonly
scalar `xrescale' = (r(max) - r(min)) / `xrescale'
if `xrescale'>=. scalar `xrescale' = 1
qui replace Y = Y + `dY'*`xrescale' if cY<. `in'
qui replace X = X + `dX'*`xrescale' if cX<. `in'
}
return scalar xrescale = `xrescale'
end
program _rotate
args r in
if `r'==0 exit
local r = `r' * _pi / 180
capt confirm var X2, exact
if _rc==1 exit 1
local hasXY2 = _rc==0
tempname min max
foreach v in Y X {
tempname `v'mid
_get_minmax `v' `in'
scalar ``v'mid' = (r(max) + r(min)) / 2
}
capt confirm var cX, exact
if _rc==1 exit 1
if _rc==0 { // lock non-rotating shapes
tempvar dY dX
qui gen double `dY' = Y - cY if cY<. `in'
qui gen double `dX' = X - cX if cX<. `in'
qui replace Y = cY if cY<. `in'
qui replace X = cX if cX<. `in'
}
tempvar y x
qui gen double `y' = Y - `Ymid' `in'
qui gen double `x' = X - `Xmid' `in'
qui replace Y = (`x' * sin(`r') + `y' * cos(`r')) + `Ymid' `in'
qui replace X = (`x' * cos(`r') - `y' * sin(`r')) + `Xmid' `in'
if `hasXY2' {
qui replace `y' = Y2 - `Ymid' `in'
qui replace `x' = X2 - `Xmid' `in'
qui replace Y2 = (`x' * sin(`r') + `y' * cos(`r')) + `Ymid' `in'
qui replace X2 = (`x' * cos(`r') - `y' * sin(`r')) + `Xmid' `in'
}
if "`dY'"!="" { // restore non-rotating shapes
qui replace Y = Y + `dY' if cY<. `in'
qui replace X = X + `dX' if cX<. `in'
}
end
program _inset
gettoken BELOW 0 : 0
gettoken PROJECT 0 : 0
gettoken PROJOPTS 0 : 0
gettoken ANGLE 0 : 0
gettoken inum 0 : 0
gettoken p 0 : 0
gettoken cframe 0 : 0, parse(" ,")
syntax [, inset(str asis) ]
local 0 `"`inset'"'
// parse layers
_parse expand layer lg : 0
if `"`lg_if'"'!="" {
di as err "global {bf:if} not allowed in inset()"
exit 198
}
if `"`lg_in'"'!="" {
di as err "global {bf:in} not allowed in inset()"
exit 198
}
local 0 , `lg_op'
// options
syntax [, below nobox BOX2(str)/*
*/ BACKground BACKground2(str) NOPROJect PROJect PROJect2(str)/*
*/ ANGle(numlist max=1) rotate(numlist max=1)/*
*/ SIze(numlist max=1 >=0 missingok) Margin(str) POSition(str)/*
*/ XMargin(numlist max=1 >=0 <=50) YMargin(numlist max=1 >=0 <=50) * ]
// exit if first try and not below
if `BELOW' & "`below'"=="" {
c_local ins_`inum' 0 // process inset later
exit
}
// parse further options
_collect_repeated TItle `"`macval(options)'"' // title_n, title_1 ...
local TITLE
forv i = 1/`title_n' {
local TITLE `TITLE' `title_`i''
}
if "`size'"=="" local size 30
mata: _geo_parse_marginexp("margin", `"`margin'"', 0, "`box'"=="")
if `"`position'"'=="" local position 0 // center
else _parse_position `position'
if "`xmargin'"=="" {
if `title_n' local xmargin 3
else local xmargin 0
}
if "`ymargin'"=="" {
if `title_n' local ymargin 3
else local ymargin 0
}
if "`angle'"=="" local angle `rotate'
if "`angle'"=="" local angle `ANGLE'
if "`noproject'"=="" {
if `"`project2'"'!="" _parse_project `project2'
else if `"`PROJECT'"'!="" {
local project2 `"`PROJECT'"'
local project_opts `"`PROJOPTS'"'
}
else if "`project'"!="" _parse_project
if `"`project2'"'!="" local project project
}
else local project2
if `"`background2'"'!="" local background background
if "`background'"!="" _parse_background `project', `background2'
_collect_repeated GRID2 `"`macval(options)'"' // grid_n, grid_1 ...
_options_not_allowed, `options'
// prepare box
local plots
if "`box'"=="" {
_inset_box `p', `box2' // plot, p, box_pad
local plots `plots' `plot'
}
else qui set obs `=_N + 6'
// prepare background
local N1 = _N + 1
if "`background'"!="" {
_background `p' `bg_n' `"`bg_feat'"' `"`background2'"'
local plots `plots' `plot'
}
// process layers
local N2 = _N + 1
forv i = 1/`layer_n' {
_parse_layer "`cframe'" `layer_`i'' // plottype, lframe, layer
_geoplot_`plottype' . `p' `lframe' `layer' // plot, p
local plots `plots' `plot'
}
if _N<`N2' { // empty inset
c_local plot
exit
}
// background and grid
if "`background'"!="" {
_background_fillin l `=`N1'+1' `bg_n' "`bg_pad'" `bg_limits'
}
forv i = 1/`grid_n' {
capt n _grid `p', `grid_`i'' // plot, p, grid_lbls
if _rc==1 exit 1
if _rc {
di as err "error in grid()"
exit _rc
}
local plots `plots' `plot'
}
if `grid_n' & "`background'"!="" {
capt numlist "`bg_pad'", range(>=0)
if _rc==1 exit 1
if !_rc {
foreach l of local bg_limits {
if `l'<. continue
_background_fillin l `=`N1'+1' `bg_n' 0 `bg_limits'
continue, break
}
}
}
// project and rotate
qui _project `"`project2'"' `"`project_opts'"' "in `N1'/l"
_rotate `angle' "in `N1'/l"
// returns
c_local ins_`inum' 1
c_local ins_`inum'_ti `"`TITLE'"'
c_local ins_`inum'_s `size'
c_local ins_`inum'_pos `position'
c_local ins_`inum'_mrg `margin'
c_local ins_`inum'_xm `xmargin'
c_local ins_`inum'_ym `ymargin'
c_local plot `plots'
c_local p `p'
end
program _inset_box
gettoken p 0 : 0, parse(" ,")
tempname BOX
frame create `BOX'
frame `BOX' {
qui set obs 6
qui gen _X = .
qui gen _Y = .
}
__geoplot_layer area . `p' `BOX'`0'
c_local p `p'
c_local plot `plot'
end
program _inset_add_title
gettoken p 0 : 0
gettoken a 0 : 0
gettoken b 0 : 0
su X in `a'/`b', meanonly
local X0 = r(min)
local X1 = r(max)
su Y in `a'/`b', meanonly
local Y0 = r(min)
local Y1 = r(max)
_collect_repeated TItle `"`macval(0)'"' // title_n, title_1 ...
local TITLE
forv i = 1/`title_n' {
_parse_titleopt `i', `title_`i''
if `"`title_`i''"'=="" continue
_set_xy_from_pos Xti Yti `X0' `X1' `Y0' `Y1' `title_`i'_pos'
local TITLE `TITLE' text(`Yti' `Xti' `title_`i'', `title_`i'_opts')
}
if `"`TITLE'"'!="" {
_geoplot_scatteri . `p' . ., ms(i) `TITLE'
}
c_local plot `plot'
c_local p `p'
end
program _zoom
gettoken n 0 : 0
gettoken refdim 0 : 0
gettoken p 0 : 0
_parse comma LAYERS 0 : 0
syntax [, zoom(str) ]
if `"`zoom'"'=="" exit // zoom() specified, but empty
// syntax: <layers>: scale [offset angle] [, options]
// - parse <layers>
numlist "1/`LAYERS'"
local LAYERS `r(numlist)'
_on_colon_parse `zoom'
local args `"`s(after)'"'
numlist `"`s(before)'"', int range(>0)
local layers `r(numlist)'
local layers: list layers & LAYERS
if "`layers'"=="" exit // no (available) layers selected
c_local LAYERS: list LAYERS - layers // update list of remaining layers
// - parse rest
_parse comma args 0 : args
numlist `"`args'"', max(3)
local args `r(numlist)'
gettoken scale args : args
gettoken offset args : args
gettoken angle args : args
if "`scale'"=="" local scale 1
if "`offset'"=="" local offset 0
if "`angle'"=="" local angle 0