PHP script to geocode comunidad valenciana list of VUT

parent 129dfe18
<?php
$csv_filename = '../data/original/190302_viviendas-turisticas-comunidad-valenciana_valencia.csv';
$out_filename = '../data/output/190302_viviendas-turisticas-comunidad-valenciana_valencia_geocoded.csv';
$line_length = "4096"; // max line lengh (increase in case you have longer lines than 1024 characters)
$delimiter = ","; // field delimiter character
$enclosure = '"'; // field enclosure character
// Geocoding script using Nominatim http://nominatim.openstreetmap.org/
// to get coordinates using City, Country, street name, house number and Postal Code
function geocode_it( $country='',$state='',$city='',$street_name='',$house_number='') {
// use nominatim geocoding service to get coords
$q = "https://nominatim.openstreetmap.org/search?format=json&country=".urlencode($country)."&city=".urlencode($city)."&state=".urlencode($state)."&street=".urlencode($house_number." ".$street_name)."&limit=1";
// we use curl instead of file_get_contents for not being blocked by nominatim
//$results_json = file_get_contents($q);
$headers = [
'X-Apple-Tz: 0',
'X-Apple-Store-Front: 143444,12',
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Encoding: gzip, deflate',
'Accept-Language: en-US,en;q=0.5',
'Cache-Control: no-cache',
'Content-Type: application/x-www-form-urlencoded; charset=utf-8',
'Host: www.example.com',
'Referer: http://www.example.com/index.php', //Your referrer address
'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:28.0) Gecko/20100101 Firefox/28.0',
'X-MicrosoftAjax: Delta=true'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$q);
//curl_setopt($ch, CURLOPT_POST, 1);
//curl_setopt($ch, CURLOPT_POSTFIELDS,$vars); //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$curl_response = curl_exec ($ch);
curl_close ($ch);
$results = json_decode($curl_response,TRUE); // if second parameter is set to TRUE, the output is ass. array
if ( !array_key_exists('0',$results) ) {
$q = "https://nominatim.openstreetmap.org/search?format=json&q=".urlencode($country." ".$city." ".$street_name. " " .$street_number). "&limit=1";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$q);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$curl_response = curl_exec ($ch);
curl_close ($ch);
$results = json_decode($curl_response,TRUE);
}
if ( array_key_exists('0',$results) )
return $results[0];
return;
}
// open the original file
$fp = fopen($csv_filename,'r');
// get data and store it in array
if ( $fp !== FALSE ) { // if the file exists and is readable
// data array generation
$data = array();
$output = array();
$output[] = '"Signatura","Municipio","Provincia","Address","Teléfono","lat","lon"'.PHP_EOL;
$line = -1;
while ( ($fp_csv = fgetcsv($fp,$line_length,$delimiter,$enclosure)) !== FALSE ) { // begin main loop
$line++;
if ( $line == 0 )
continue;
// debug
//if ( $line == 6 )
// break;
echo "Geocoding register ".$line."...";
echo "\r\n";
$country = 'Spain';
$state = 'Valencia';
$city = 'Valencia';
$address = $fp_csv[3];
$pattern = '/^[a-zA-Z]* ([^,]*), .*/i';
$rep = '${1}';
$street_name = preg_replace($pattern, $rep, $address);
$pattern = '/^[^,]*,[^0-9]*([0-9]*).*/i';
$rep = '${1}';
$street_number = preg_replace($pattern, $rep, $address);
$geo = geocode_it($country,$state,$city,$street_name,$street_number);
$lat = '';
if ( $geo['lat'] != '' )
$lat = $geo['lat'];
$lon = '';
if ( $geo['lon'] != '' )
$lon = $geo['lon'];
if ( $lat == '' || $lon == '' )
echo 'Geocoding failed for record '.$line.'.';
else
echo 'Geocoding succeded for record '.$line.'.';
echo "\r\n";
$output[] = '"'.$fp_csv[0].'","'.$fp_csv[1].'","'.$fp_csv[2].'","'.$fp_csv[3].'","'.$fp_csv[4].'","'.$lat.'","'.$lon.'"'.PHP_EOL;
}
fclose($fp);
// open the output file to write
$fp = fopen($out_filename,'w');
// get data and store it in array
if ( $fp !== FALSE ) { // if the file exists and is readable
echo 'Writing geodata to '.$out_filename;
echo "\r\n";
foreach ( $output as $o ) {
fwrite($fp,$o);
}
fclose($fp);
echo 'Geocoding ended.';
echo "\r\n";
}
}
?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment