-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Foot traffic data import and aggregations #479
Open
antoniocarlon
wants to merge
11
commits into
master
Choose a base branch
from
474-Import_foot_traffic_data_and_aggregations
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
644d03d
Foot traffic data import and aggregations
c7aace2
Adding PLPython to the docker image
b8df992
Created index and clustered materialized view
4c8f6b1
Created higher level zooms
68a5a1b
Added metadata table and column comments
0761cce
Created indexes on metadata table
248e147
Created indexes on metadata table
6aaabf7
Importing data into ClickHouse
7306b10
Importing data into ClickHouse
a905a1b
Importing data into ClickHouse
401807d
Import mechanism fot ClickHouse
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import os | ||
import urllib.request | ||
from luigi import Task, LocalTarget | ||
from lib.logger import get_logger | ||
from tasks.util import (classpath, unqualified_task_id) | ||
from tasks.foot_traffic.quadkey import tile2bounds, quadkey2tile | ||
|
||
LOGGER = get_logger(__name__) | ||
|
||
|
||
class DownloadData(Task): | ||
|
||
URL = 'YOUR_URL_HERE' | ||
|
||
def run(self): | ||
self.output().makedirs() | ||
urllib.request.urlretrieve(self.URL, self.output().path) | ||
|
||
def output(self): | ||
return LocalTarget(os.path.join('tmp', classpath(self), | ||
unqualified_task_id(self.task_id) + '.csv')) | ||
|
||
|
||
class AddLatLngData(Task): | ||
def requires(self): | ||
return DownloadData() | ||
|
||
def _point_position(self, z, x, y): | ||
lon0, lat0, lon1, lat1 = tile2bounds(z, x, y) | ||
return (lon0 + lon1) / 2, (lat0 + lat1) / 2 | ||
|
||
def run(self): | ||
with open(self.output().path, 'w') as outfile, open(self.input().path, 'r', encoding='utf-8') as infile: | ||
i = 0 | ||
for line in infile: | ||
line = line.split(',') | ||
quadkey = line[0] | ||
z, x, y = quadkey2tile(quadkey) | ||
lon, lat = self._point_position(z, x, y) | ||
line.append(lon) | ||
line.append(lat) | ||
outline = [line[0]] + [lon] + [lat] + [line[1]] + [line[2]] + [line[3]] | ||
outfile.write(','.join([str(c) for c in outline])) | ||
|
||
i = i + 1 | ||
if i % 10000 == 0: | ||
LOGGER.info('Written {i} lines'.format(i=i)) | ||
|
||
def output(self): | ||
return LocalTarget(os.path.join('tmp', classpath(self), | ||
unqualified_task_id(self.task_id) + '.csv')) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import requests | ||
from luigi import Task | ||
from lib.logger import get_logger | ||
from tasks.foot_traffic.data_file import AddLatLngData | ||
|
||
LOGGER = get_logger(__name__) | ||
CLICKHOUSE_HOST = '172.17.0.1' | ||
CLICKHOUSE_PORT = 8123 | ||
DATABASE_NAME = 'foot_traffic' | ||
TABLE_NAME = 'foot_traffic' | ||
QUADKEY_FIELD = 'quadkey' | ||
LONGITUDE_FIELD = 'lon' | ||
LATITUDE_FIELD = 'lat' | ||
DATE_FIELD = 'ftdate' | ||
HOUR_FIELD = 'fthour' | ||
VALUE_FIELD = 'val' | ||
|
||
|
||
def execute_query(query): | ||
uri = 'https://{host}:{port}/?query={query}'.format(host=CLICKHOUSE_HOST, port=CLICKHOUSE_PORT, query=query) | ||
response = requests.get(uri) | ||
if response.status_code != requests.codes.ok: | ||
raise RuntimeError('Received status code {code}'.format(code=response.status_code)) | ||
return response.text | ||
|
||
|
||
class ImportData(Task): | ||
def requires(self): | ||
return AddLatLngData() | ||
|
||
def _create_table(self): | ||
query = ''' | ||
CREATE DATABASE IF NOT EXISTS "{database}"; | ||
'''.format( | ||
database=DATABASE_NAME | ||
) | ||
execute_query(query) | ||
|
||
query = ''' | ||
CREATE TABLE IF NOT EXISTS {database}.{table} ( | ||
{quadkey} String, | ||
{longitude} Float64, | ||
{latitude} Float64, | ||
{date} Date, | ||
{hour} UInt8, | ||
{value} UInt16 | ||
) ENGINE = MergeTree({date}, ({quadkey}, {longitude}, {latitude}, {date}, {hour}), 8192) | ||
'''.format( | ||
database=DATABASE_NAME, | ||
table=TABLE_NAME, | ||
quadkey=QUADKEY_FIELD, | ||
longitude=LONGITUDE_FIELD, | ||
latitude=LATITUDE_FIELD, | ||
date=DATE_FIELD, | ||
hour=HOUR_FIELD, | ||
value=VALUE_FIELD, | ||
) | ||
execute_query(query) | ||
|
||
def run(self): | ||
self._create_table() | ||
|
||
def complete(self): | ||
query = ''' | ||
EXISTS TABLE {database}.{table} | ||
'''.format( | ||
database=DATABASE_NAME, | ||
table=TABLE_NAME, | ||
) | ||
exists = execute_query(query) | ||
LOGGER.error(exists) | ||
return exists == '1' |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'requests' imported but unused