-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOSM_get.php
171 lines (153 loc) · 5.52 KB
/
OSM_get.php
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
<?php
/**
* Get OpenStreetMap dumps, and put it at data/dumps_osm.
* @example php src/OSM_get.php 59470:BR # saves data/dumps_osm/BR_wdElements.csv
* @example php src/OSM_get.php BR # same effect, automatically gets relation
* @example php src/OSM_get.php BR_states # saves by stats (beta test feature)
* @example php src/OSM_get.php all # get all countries (to be implented, very slow)
*/
$saveFolder = realpath( dirname(__FILE__)."/../data" );
$URL_Overpass = 'http://overpass-api.de/api/interpreter?data=';
$TPL_Overpass = <<<EOT
[out:csv(::type,::id,wikidata,name)] ;
relation (_MASK_RELATION_ID_) -> .c ;
.c map_to_area -> .myarea ;
(
node (area.myarea) [wikidata];
way (area.myarea) [wikidata];
relation (area.myarea) [wikidata];
);
out meta ;
EOT;
if ($argc >= 2) {
ERRprint( "\n Using terminal args" );
array_shift($argv);
$R = $argv;
if (count($R)==1) {
if ($R[0]=='all') {
// All countries, automatic by countries.csv
$isoCountries = getCsvFields("$saveFolder/dumps_wd/countries.csv",['iso2','osm_relid']);
$R=[];
foreach ($isoCountries as $nada => $r) {
$R[] = "$r[osm_relid]:$r[iso2]";
}
} elseif ( preg_match('/^[a-z][a-z]$/i',$R[0]) ) {
// One country, automatic by its ISO code
ERRprint( "\n Using country=$R[0]" );
$R[0] = strtoupper($R[0]);
$isoCountries = getCsvFields("$saveFolder/dumps_wd/countries.csv",['iso2','osm_relid']);
$ok=false;
foreach ($isoCountries as $nada => $r) if ($r['iso2']==$R[0]) {
$R=["$r[osm_relid]:$R[0]"]; $ok=true; break;
}
if (!$ok) ERRdie('args',"no country $R[0]. Use 3166-1 alfa-2 country code.");
} elseif ( $R[0]=='BR_states' ) {
// One country and its states, automatic by country's ISO code
ERRprint( "\n Using datasets-br/state-codes" );
$R = [];
$UFs_url = 'https://raw.githubusercontent.com/datasets-br/state-codes/master/data/br-state-codes.csv';
foreach (getCsvFields($UFs_url,['subdivision','extinction']) as $r) if ($r && empty($r['extinction'])) {
$uf = $r['subdivision'];
$u = "https://raw.githubusercontent.com/datasets-br/state-codes/master/data/dump_wikidata/$uf.json";
$j = json_decode(file_get_contents($u),true);
$qid = $j['id'];
$R[] = $j['claims']['P402'][0]['value'] . ":BR_$uf";
} // for
} else
ERRdie('args',"invalid argument");
} // if count=1
} else
ERRdie('args',"please check how to use arguments.");
foreach ($R as $arg) {
list($r,$name) = explode(':',$arg);
$u2 = overpassGet($r);
print "\n - $name - relation $r to Overpass...";
$tsv = file_get_contents($u2);
$tsv_n = substr_count( trim($tsv), "\n" );
if ($tsv_n>1) {
print " $tsv_n lines read. Saving and waiting... ";
file_put_contents( "$saveFolder/dumps_osm/{$name}_elements.csv", tsv2csv($tsv) );
} else print " ops, some error... last_Overpass=\n$last_Overpass\n";
sleep(rand(1,4));
print date('h:i:s') ;
}
die("\n");
///// LIB
function overpassGet($osm_rel_id) {
global $URL_Overpass;
global $TPL_Overpass;
global $last_Overpass;
$last_Overpass = str_replace('_MASK_RELATION_ID_',$osm_rel_id,$TPL_Overpass);
return $URL_Overpass.urlencode($last_Overpass);
}
function ERRprint($msg) {
fwrite(STDERR, $msg);
}
function ERRset($fname,$msg0) {
global $ERR;
$msg = "ERROR, $msg0 for $fname.";
ERRprint($msg);
$ERR[] = "\n$fname,\"$msg\"";
return 0;
}
function ERRdie($fname,$msg0) {
ERRset($fname,$msg0);
die("\n");
}
function tsv2csv($full) {
$full = preg_replace_callback(
'/\t([^\t]*)\n/',
function ($m) { $x=trim($m[1],'" '); return ",\"$x\"\n"; },
$full
);
return str_replace("\t", ',', $full);
}
//// LIB2 - from https://github.com/ppKrauss/php-little-utils
/**
* Check if is a filename string, not a CSV/XML/HTML/markup string.
* @param $input string of filename or markup code.
* @param $flenLimit integer 0 or limit of filename length.
* @param $keyStr string '<' for XML, "\n" for CSV.
* @return boolean true when is filename or path string, false when markup.
*/
function isFile($input,$flenLimit=600,$keyStr='<') {
return strrpos($input,$keyStr)==false && (!$flenLimit || strlen($input)<$flenLimit);
}
/**
* Standard "get array from CSV", file or CSV-string.
* CSV conventions by default options of the build-in str_getcsv() function.
* @param $f string file (.csv) with path or CSV string (with more tham 1 line).
* @param $flenLimit integer 0 or limit of filename length (as in isFile function).
* @return array of arrays.
* @use isFile() at check.php
*/
function getCsv($f,$flenLimit=600) {
return array_map(
'str_getcsv',
isFile($f,$flenLimit,"\n")? file($f): explode($f,"\n")
);
}
/**
* Get data (array of associative arrays) from CSV file, only the listed keys.
* @param $f string file (.csv) with path or CSV string (with more tham 1 line).
* @param $flist array of column names, or NULL for "all columns".
* @param $outJSON boolean true for JSON output.
* @param $flenLimit integer 0 or limit of filename length (as in isFile function).
* @return mix JSON or array of associative arrays.
*/
function getCsvFields($f,$flist=NULL,$outJSON=false,$flenLimit=600) {
$t = getCsv($f,$flenLimit);
$thead = array_shift($t);
$r = [];
foreach($t as $x) {
$a = array_combine($thead,$x);
if ($flist===NULL)
$r[] = $a;
elseif (isset($a[$flist[0]])) { // ~ array_column()
$tmp = []; // NEED OPTIMIZE WITH array_intersection!
foreach ($flist as $g) $tmp[$g] = $a[$g];
$r[] = $tmp;
}
}
return $outJSON? json_encode($r): $r;
}