From d1dc81165a30c2f45bf9fabf5e01ca984fdb3783 Mon Sep 17 00:00:00 2001 From: James Baster Date: Sat, 9 Jul 2016 11:24:45 +0100 Subject: [PATCH] Inital Commit --- .gitignore | 3 + LICENSE.md | 28 +++++ README.md | 4 + bin/CodePointOpen/getPostCode.php | 27 +++++ .../loadDataToFileDataAdaptor.php | 26 ++++ composer.json | 20 ++++ composer.lock | 52 ++++++++ data/codepointopen/empty.file.txt | 0 src-bin/bootstrap.php | 20 ++++ .../OSData/BaseDataAdaptor.php | 16 +++ .../OSData/BaseDataService.php | 29 +++++ .../CodePointOpen/CodePointOpenService.php | 58 +++++++++ .../OSData/CodePointOpen/FileDataAdaptor.php | 111 ++++++++++++++++++ .../CodePointOpen/InterfaceDataAdaptor.php | 23 ++++ .../OSData/CodePointOpen/PostCodeData.php | 50 ++++++++ 15 files changed, 467 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE.md create mode 100644 README.md create mode 100644 bin/CodePointOpen/getPostCode.php create mode 100644 bin/CodePointOpen/loadDataToFileDataAdaptor.php create mode 100644 composer.json create mode 100644 composer.lock create mode 100644 data/codepointopen/empty.file.txt create mode 100644 src-bin/bootstrap.php create mode 100644 src/JMBTechnologyLimited/OSData/BaseDataAdaptor.php create mode 100644 src/JMBTechnologyLimited/OSData/BaseDataService.php create mode 100644 src/JMBTechnologyLimited/OSData/CodePointOpen/CodePointOpenService.php create mode 100644 src/JMBTechnologyLimited/OSData/CodePointOpen/FileDataAdaptor.php create mode 100644 src/JMBTechnologyLimited/OSData/CodePointOpen/InterfaceDataAdaptor.php create mode 100644 src/JMBTechnologyLimited/OSData/CodePointOpen/PostCodeData.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..60961df --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/data/codepointopen/*.csv +.DS_Store +/vendor diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..b2e6881 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,28 @@ +Copyright (c) 2014-2016, JMB Technology Ltd +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + + Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + Redistributions in binary form must reproduce the above copyright notice, this + list of conditions and the following disclaimer in the documentation and/or + other materials provided with the distribution. + + Neither the name of JMB Technology Ltd nor any of their products (including + but not limited to OpenACalendar, HasACalendar and OpenTechCalendar) nor the + names of any contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..a38e4cb --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# OSData + +An PHP Library for working with Ordnance Survey Open Data + diff --git a/bin/CodePointOpen/getPostCode.php b/bin/CodePointOpen/getPostCode.php new file mode 100644 index 0000000..8dab5d2 --- /dev/null +++ b/bin/CodePointOpen/getPostCode.php @@ -0,0 +1,27 @@ +getPostcode($argv[1]); + +if ($postCode) { + print "Lat,Lng: ". $postCode->getLat().",".$postCode->getLng()."\n"; +} else { + print "Not Found\n"; +} + + diff --git a/bin/CodePointOpen/loadDataToFileDataAdaptor.php b/bin/CodePointOpen/loadDataToFileDataAdaptor.php new file mode 100644 index 0000000..0442b32 --- /dev/null +++ b/bin/CodePointOpen/loadDataToFileDataAdaptor.php @@ -0,0 +1,26 @@ +loadData($inDir); + +print "Done"; + + diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..45bf01b --- /dev/null +++ b/composer.json @@ -0,0 +1,20 @@ +{ + "name": "jmbtechnologylimited/osdata", + "type": "library", + "description": "for working with Ordnance Survey Open Data", + "keywords": [], + "homepage": "https://github.com/JMB-Technology-Limited/OSData", + "license": "BSD", + "authors": [ ], + "require": { + "php": ">=5.3.0", + "jmbtechnologylimited/osmaths": "1.0.*" + }, + "autoload": { + "psr-0": { + "JMBTechnologyLimited": "src" + } + }, + "require-dev": { + } +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..5b08ef0 --- /dev/null +++ b/composer.lock @@ -0,0 +1,52 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" + ], + "hash": "35859cb9f17cb71a0241a3d715afed94", + "content-hash": "31328e68cb8a18d71c1f1309e87440ec", + "packages": [ + { + "name": "jmbtechnologylimited/osmaths", + "version": "v1.0.0", + "source": { + "type": "git", + "url": "https://github.com/JMB-Technology-Limited/OSMaths.git", + "reference": "0ec1a8f2731378f23859e40892f580072baf323d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/JMB-Technology-Limited/OSMaths/zipball/0ec1a8f2731378f23859e40892f580072baf323d", + "reference": "0ec1a8f2731378f23859e40892f580072baf323d", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "autoload": { + "psr-0": { + "JMBTechnologyLimited": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD" + ], + "description": "conversations around the coordinate systems used for Ordnance Survey Open Data only", + "homepage": "https://github.com/JMB-Technology-Limited/OSMaths", + "time": "2016-07-09 10:03:47" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": { + "php": ">=5.3.0" + }, + "platform-dev": [] +} diff --git a/data/codepointopen/empty.file.txt b/data/codepointopen/empty.file.txt new file mode 100644 index 0000000..e69de29 diff --git a/src-bin/bootstrap.php b/src-bin/bootstrap.php new file mode 100644 index 0000000..1a6a5b0 --- /dev/null +++ b/src-bin/bootstrap.php @@ -0,0 +1,20 @@ +dataAdaptor = $dataAdaptor; + } + + + /** + * @return boolean Was this successful? + */ + public function openConnection() + { + + } + + public function closeConnection() + { + + } + + public function loadData($sourceDir) + { + $this->dataAdaptor->loadData($sourceDir); + } + + /** @return PostCodeData */ + public function getPostcode($postcode) + { + return $this->dataAdaptor->getPostcode($postcode); + } + +} + diff --git a/src/JMBTechnologyLimited/OSData/CodePointOpen/FileDataAdaptor.php b/src/JMBTechnologyLimited/OSData/CodePointOpen/FileDataAdaptor.php new file mode 100644 index 0000000..4d01e11 --- /dev/null +++ b/src/JMBTechnologyLimited/OSData/CodePointOpen/FileDataAdaptor.php @@ -0,0 +1,111 @@ +dir = $dir; + } + + + public function loadData($sourceDir) { + + $handleDir = null; + + if ($handleDir = opendir($sourceDir)) { + while (false !== ($entry = readdir($handleDir))) { + if ($entry != '.' && $entry != '..' && substr($entry, -4) == '.csv') { + + ## Setup + $outputfiles = array(); + + ## Stage 1: loop over each file + print "Starting " . $entry . "\n"; + if (($handle = fopen($sourceDir . DIRECTORY_SEPARATOR . $entry, "r")) !== FALSE) { + while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { + + if (count($data) > 3) { + + $postcode = strtoupper(str_replace(" ", "", $data[0])); + $latLng =(new EastingNorthing($data[2], $data[3]))->toLatLngOSGB36()->toLatLngWGS84(); + $postcodeTwoChars = substr($postcode, 0, 2); + + if (!isset($outputfiles[$postcodeTwoChars])) { + $outputfiles[$postcodeTwoChars] = fopen($this->dir . DIRECTORY_SEPARATOR . $postcodeTwoChars . ".tmp.csv", 'w'); + if (!$outputfiles[$postcodeTwoChars]) { + die("Could not open output file: " . $this->dir . DIRECTORY_SEPARATOR . $postcodeTwoChars . ".tmp.csv\n\n"); + } + } + fwrite($outputfiles[$postcodeTwoChars], '"' . $postcode . '",' . $latLng->getLat() . ',' . $latLng->getLng() . "\n"); + + } + } + fclose($handle); + } + ## Stage 2: close all open files + print "Cleaning up " . $entry . "\n"; + foreach ($outputfiles as $key => $fh) { + fclose($fh); + } + + ## Stage 3: Copy + print "Installing " . $entry . "\n"; + foreach ($outputfiles as $key => $fh) { + rename($this->dir . DIRECTORY_SEPARATOR . $key . ".tmp.csv", $this->dir . DIRECTORY_SEPARATOR . $key . ".csv"); + } + + + } + } + closedir($handleDir); + return true; + } + + return false; + + } + + + /** @return PostCodeData */ + public function getPostcode($postcode) + { + + $postcode = strtoupper(str_replace(" ","",$postcode)); + $postcodeTwoChars = substr($postcode, 0,2); + + $ourfile = $this->dir.DIRECTORY_SEPARATOR.$postcodeTwoChars.'.csv'; + + if (is_file($ourfile)) { + if (($handle = fopen($ourfile, "r")) !== FALSE) { + while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { + if (count($data) > 2 && $data[0] == $postcode) { + fclose($handle); + return new PostCodeData($data[1], $data[2]); + } + } + fclose($handle); + } + } + + return null; + + } +} diff --git a/src/JMBTechnologyLimited/OSData/CodePointOpen/InterfaceDataAdaptor.php b/src/JMBTechnologyLimited/OSData/CodePointOpen/InterfaceDataAdaptor.php new file mode 100644 index 0000000..14090ad --- /dev/null +++ b/src/JMBTechnologyLimited/OSData/CodePointOpen/InterfaceDataAdaptor.php @@ -0,0 +1,23 @@ +lat = $lat; + $this->lng = $lng; + } + + /** + * @return mixed + */ + public function getLat() + { + return $this->lat; + } + + /** + * @return mixed + */ + public function getLng() + { + return $this->lng; + } + + + + +}