Last active
June 27, 2016 07:50
-
-
Save rtsisyk/2e8f482f87aa5ec670d326f19b7fe92e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env tarantool | |
| package.path = "../?/init.lua;../?.lua;./?/init.lua;./?.lua;"..package.path | |
| package.cpath = "../?.so;../?.dylib;./?.so;./?.dylib"..package.cpath | |
| box.cfg({logger = 'tarantool.log'}) | |
| local httpc = require('http.client') | |
| local json = require('json') | |
| local yaml = require('yaml') | |
| local gis = require('gis') | |
| gis.install() -- creates system tables in Tarantool, e.g. spatial_ref_sys | |
| local function tocube(coords) | |
| return gis.Point(coords, 4326):transform(4328) -- lonlat to geocentric (3D) | |
| end | |
| box.once("data", function() | |
| print('Creating spaces...') | |
| local postoffices = box.schema.space.create("postoffices") | |
| postoffices:create_index('primary', { type = 'HASH', parts = {1, 'num'}}) | |
| postoffices:create_index('spatial', { type = 'RTREE', parts = {2, 'array'}, | |
| unique = false, dimension = 3}) | |
| print('Downloading source data...') | |
| local URL = 'http://api.data.mos.ru/v1/datasets/1095/rows' | |
| local sourcedata = json.decode(httpc.get(URL).body) | |
| print('Populating database...') | |
| for _, info in pairs(sourcedata) do | |
| local postalcode = tonumber(info.Cells.PostalCode) | |
| local lon = tonumber((info.Cells.X_WGS84:gsub(',', '.'))) | |
| local lat = tonumber((info.Cells.Y_WGS84:gsub(',', '.'))) | |
| local address = info.Cells.Address | |
| postoffices:replace({postalcode, tocube({lon, lat}):totable(), {lon, lat}, address}) | |
| end | |
| end) | |
| local function nearby(coords, count) | |
| local point = tocube(coords) | |
| for _, office in box.space.postoffices.index.spatial:pairs(point:totable(), | |
| { iterator = 'neighbor' }):take(count) do | |
| print(json.encode({ | |
| PostalCode = office[1]; | |
| Address = office[4]; | |
| Distance = math.ceil(point:distance(tocube(office[3]))) | |
| })) | |
| end | |
| end | |
| nearby({37.479407, 55.862488}, 5) | |
| print('--') | |
| nearby({37.537407, 55.796782}, 5) | |
| os.exit(0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| test$ ./PostOfficesPrimer.lua | |
| {"Address":"Фестивальная улица, дом 39","Distance":463,"PostalCode":125195} | |
| {"Address":"Валдайский проезд, дом 8, строение 2","Distance":764,"PostalCode":125445} | |
| {"Address":"Петрозаводская улица, дом 9, корпус 2","Distance":985,"PostalCode":125502} | |
| {"Address":"улица Ляпидевского, дом 14","Distance":1154,"PostalCode":125581} | |
| {"Address":"Ленинградское шоссе, дом 84, корпус 2","Distance":1289,"PostalCode":125565} | |
| -- | |
| {"Address":"Ленинский проспект, дом 41","Distance":150,"PostalCode":119334} | |
| {"Address":"Ленинградский проспект, дом 56","Distance":168,"PostalCode":125167} | |
| {"Address":"улица Черняховского, дом 6","Distance":696,"PostalCode":125319} | |
| {"Address":"улица Усиевича, дом 16","Distance":1232,"PostalCode":125190} | |
| {"Address":"Ленинградский проспект, дом 69","Distance":1357,"PostalCode":125057} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment