Skip to content

Commit

Permalink
Reorganize
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkohei13 committed May 19, 2016
1 parent b09e752 commit d36fcb6
Show file tree
Hide file tree
Showing 2 changed files with 211 additions and 0 deletions.
88 changes: 88 additions & 0 deletions navigator-parser.php
@@ -0,0 +1,88 @@
<?php


/*
Parses data from Garmin navigator file:
20.17920, 59.96930, "Lemland, Herröskatan"
20.60189, 60.02766, "Föglö, Hastersboda"
20.53805, 60.08624, "Föglö, Jyddö"
File source: http://rslh.info/index.php?option=com_content&view=article&id=48&Itemid=60
*/

$allData = Array();

$filename = "rawdata/tornit.csv";

$dataString = file_get_contents($filename);

$dataRows = explode("\n", $dataString);

foreach ($dataRows as $key => $row) {
handleTower($row);
}

writeFile();


function handleTower($row)
{
$cells = explode(",", $row);

$lon = trim($cells[0]);
$lat = trim($cells[1]);

$muni = trim($cells[2]);
$tower = trim($cells[3]); // suppressed errors from missing comma between muni and tower

$tempMuni = trim($muni, "\"");
$tempTower = trim($tower, "\"");
$tempLocation = $tempMuni . " " . $tempTower;
$firstWhitespace = strpos($tempLocation, " ");
$muni = substr($tempLocation, 0, $firstWhitespace);
$tower = substr($tempLocation, $firstWhitespace);

// create an id based on name
$id = sha1(($muni . $tower));

// Save to master array
global $allData;
$allData[$id]['lat'] = $lat;
$allData[$id]['lon'] = $lon;
$allData[$id]['muni'] = $muni;
$allData[$id]['tower'] = $tower;

// Save to database (test)
saveTower($id, $lat, $lon, $muni, $tower);
}

function saveTower($id, $lat, $lon, $muni, $tower)
{
// echo "$id / $lat / $lon / $muni / $tower <br />\n";
// echo "\t$lat\t$lon\twgs84\t$tower<br />\n";


}

function writeFile()
{
global $allData;
// print_r ($allData); // debug

$json = json_encode($allData);

if ($json === FALSE)
{
echo "JSON encoding failed!";
}

// echo "JSON follows: " . $json; // debug

file_put_contents("towers-autogenerated.json", $json);

echo "writeFile END";
}



123 changes: 123 additions & 0 deletions tiira-soap.php
@@ -0,0 +1,123 @@
<?php

/*
Tool to get data from Tiira-API (SOAP)
*/

// http://www.vankouteren.eu/blog/2009/03/simple-php-soap-example/

require_once "../../tiira-api.php";
if ($_GET['secret'] != $localSecret)
{
exit("secret parameter needed");
}

$masterTowers = Array();

$clientOptions = Array();
$clientOptions['login'] = $httpLogin;
$clientOptions['password'] = $httpPassword;
$clientOptions['trace'] = TRUE;

$client = new SoapClient("http://testi.tiira.fi/ws/ws/v2/soapserver.php?wsdl", $clientOptions);

$params = Array();
$params['sovellusavain'] = $apiKey;
$params['tunnus'] = $username;
$params['varmenne'] = $auth;

$params['kunta'] = "";
$params['nimi'] = "";
$params['yhdistys'] = "";

// print_r ($params);

try
{
$result = $client->haeTornit($params); // array('iTopN'=>5)
}
catch (Exception $e)
{
echo 'Caught exception: ', $e->getMessage(), "\n";
echo "REQUEST:\n" . $client->__getLastRequest() . "\n";
echo "REQUEST HEADERS:\n" . $client->__getLastRequestHeaders() . "\n";
echo "RESPONSE:\n" . $client->__getLastRequest() . "\n";
echo "RESPONSE HEADERS:\n" . $client->__getLastResponseHeaders() . "\n";

}

//print_r ($result); // debug

saveTowersAsJSON($result);


echo "\n<br />END";


function saveTowersAsJSON($data)
{
global $masterTowers; // data goes here

// Numeric id's could cause unintended sorting
$towersArr = $data->tornit->torni;

foreach ($towersArr as $key => $tower)
{
$id = sha1($tower->paikka_id);

if (strpos($tower->nimi, "lintutorni"))
{
$type = "lintutorni";
}
elseif (strpos($tower->nimi, "näkötorni") || strpos($tower->nimi, "näköalatorni"))
{
$type = "näkötorni";
}
elseif (strpos($tower->nimi, "vesitorni"))
{
$type = "vesitorni";
}
elseif (strpos($tower->nimi, "lava"))
{
$type = "lava";
}
else
{
$type = "tuntematon";
}

$masterTowers[$id]['muni'] = $tower->kunta;
$masterTowers[$id]['name'] = $tower->nimi;
$masterTowers[$id]['society'] = $tower->yhdistys;
$masterTowers[$id]['lat'] = substr($tower->n_wgs84, 0, 8);
$masterTowers[$id]['lon'] = substr($tower->e_wgs84, 0, 8);
$masterTowers[$id]['type'] = $type;
}

writeFile(); // debug
}

function writeFile()
{
global $masterTowers; // data to be written
global $localSecret; // to hide the full json file

$json = json_encode($masterTowers);

if ($json === FALSE)
{
echo "JSON encoding failed!";
}

$filename = ("data-" . $localSecret . "/towers-autogenerated.json");
file_put_contents($filename, $json);

header('Content-Type: text/html; charset=utf-8');
// print_r (masterTowers); // debug
echo "Data written to $filename";
}




0 comments on commit d36fcb6

Please sign in to comment.