Browse Source

single x and y

master
Chris 12 months ago
parent
commit
5d56205612
  1. 1
      site_builder/.gitignore
  2. BIN
      site_builder/__pycache__/main.cpython-313.pyc
  3. BIN
      site_builder/__pycache__/test_find_extrema.cpython-313-pytest-8.4.0.pyc
  4. BIN
      site_builder/__pycache__/test_find_geo_extrema.cpython-313-pytest-8.4.0.pyc
  5. BIN
      site_builder/__pycache__/test_main.cpython-313-pytest-8.4.0.pyc
  6. BIN
      site_builder/__pycache__/test_main.cpython-313.pyc
  7. BIN
      site_builder/__pycache__/test_main_unittests.cpython-313-pytest-8.4.0.pyc
  8. BIN
      site_builder/__pycache__/test_read_csv_to_dict.cpython-313-pytest-8.4.0.pyc
  9. BIN
      site_builder/__pycache__/test_translate_pos.cpython-313-pytest-8.4.0.pyc
  10. BIN
      site_builder/constants/__pycache__/fields.cpython-313.pyc
  11. 9
      site_builder/constants/fields.py
  12. 122
      site_builder/main.py
  13. 12
      site_builder/test_find_geo_extrema.py
  14. 6
      site_builder/test_read_csv_to_dict.py

1
site_builder/.gitignore vendored

@ -0,0 +1 @@
/__pycache__

BIN
site_builder/__pycache__/main.cpython-313.pyc

Binary file not shown.

BIN
site_builder/__pycache__/test_find_extrema.cpython-313-pytest-8.4.0.pyc

Binary file not shown.

BIN
site_builder/__pycache__/test_find_geo_extrema.cpython-313-pytest-8.4.0.pyc

Binary file not shown.

BIN
site_builder/__pycache__/test_main.cpython-313-pytest-8.4.0.pyc

Binary file not shown.

BIN
site_builder/__pycache__/test_main.cpython-313.pyc

Binary file not shown.

BIN
site_builder/__pycache__/test_main_unittests.cpython-313-pytest-8.4.0.pyc

Binary file not shown.

BIN
site_builder/__pycache__/test_read_csv_to_dict.cpython-313-pytest-8.4.0.pyc

Binary file not shown.

BIN
site_builder/__pycache__/test_translate_pos.cpython-313-pytest-8.4.0.pyc

Binary file not shown.

BIN
site_builder/constants/__pycache__/fields.cpython-313.pyc

Binary file not shown.

9
site_builder/constants/fields.py

@ -0,0 +1,9 @@
# Field position for data items
ID = 0
FILE = 1
LON = 2
LAT = 3
TITLE = 4
DESCRIPTION = 5
POSX = 6
POSY = 7

122
site_builder/main.py

@ -2,15 +2,19 @@ import argparse
import csv import csv
import os.path as path import os.path as path
import re import re
import constants.fields as F
importdir = 'images' importdir = 'images'
datafile = path.join(importdir, 'gps.csv') datafile = path.join(importdir, 'gps.csv')
args = None args = None
# Imported data with appended attributes as list of lists # Based on imported CSV and added with attributes:
# Items: # data with appended attributes as list of lists.
# Items ---> See constants/fields.py <---
# - ID
# - Image file name # - Image file name
# - lon, lat # - lon
# - lat
# - title, description # - title, description
# - x, y: Default tile position # - x, y: Default tile position
data = None data = None
@ -47,8 +51,15 @@ def exit_with_error(message):
def read_csv_to_dict(csv_file): def read_csv_to_dict(csv_file):
""" """
Reads a CSV file and returns a list of dictionaries, one per row. Reads a tab-separated CSV file and returns a list of dictionaries, one per row
Assumes the first row contains headers. in the full data item size. Placeholder for unfilled fields is None.
Fields:
0: ID (integer), ascending starting with '1' <-- not in CSV
1: image file name (string)
2: latitude and longitude (floats), separated by space.
3. title (string)
4. description (string)
The items must be sorted chronologically. Assumes the first row contains headers.
In error case, returns None In error case, returns None
""" """
global message global message
@ -57,6 +68,7 @@ def read_csv_to_dict(csv_file):
reader = csv.reader(f, skipinitialspace=False, delimiter='\t') reader = csv.reader(f, skipinitialspace=False, delimiter='\t')
log(f"Read lines from {csv_file}") log(f"Read lines from {csv_file}")
id = 0
for row in reader: for row in reader:
logvv(f"Processing row: {row}") logvv(f"Processing row: {row}")
@ -74,9 +86,21 @@ def read_csv_to_dict(csv_file):
if len(geo) != 2: if len(geo) != 2:
message = f'Invalid loc/lon: {row[1]}' message = f'Invalid loc/lon: {row[1]}'
values = [ row[0], float(geo[0]), float(geo[1]), row[2], row[3] ] id += 1
# Order and size must match constants.fields
# values = [id, row[0], float(geo[0]), float(geo[1]), row[2], row[3] ]
values = []
values.insert(F.ID, id)
values.insert(F.FILE, row[0])
values.insert(F.LON, float(geo[0]))
values.insert(F.LAT, float(geo[1]))
values.insert(F.TITLE, row[2])
values.insert(F.DESCRIPTION, row[3])
values.insert(F.POSX, None)
values.insert(F.POSY, None)
data.append(values) data.append(values)
logvv(f"Read row : {values}")
logvv(f"Processed item : {values}")
if len(data) == 0: if len(data) == 0:
message = f'Empty file: {csv_file}' message = f'Empty file: {csv_file}'
@ -117,18 +141,18 @@ def find_geo_extrema(data):
if len(data) == 0: if len(data) == 0:
return None return None
lonmin = lonmax = data[0][1] lonmin = lonmax = data[0][F.LON]
latmin = latmax = data[0][2] latmin = latmax = data[0][F.LAT]
for entry in data[1:]: for entry in data[1:]:
if entry[1] < lonmin: if entry[F.LON] < lonmin:
lonmin = entry[1] lonmin = entry[F.LON]
if entry[1] > lonmax: if entry[F.LON] > lonmax:
lonmax = entry[1] lonmax = entry[F.LON]
if entry[2] < latmin: if entry[F.LAT] < latmin:
latmin = entry[2] latmin = entry[F.LAT]
if entry[2] > latmax: if entry[F.LAT] > latmax:
latmax = entry[2] latmax = entry[F.LAT]
return (lonmin, lonmax, latmin, latmax) return (lonmin, lonmax, latmin, latmax)
@ -138,6 +162,7 @@ def translate_pos(min, max, pos, size):
return round((pos - min) / (max - min) * size) return round((pos - min) / (max - min) * size)
def append_tile_pos(data, geo_extrema, matrixdims): def append_tile_pos(data, geo_extrema, matrixdims):
""" """
Adds two columns with x and y position (integer) of the tile in the canvas Adds two columns with x and y position (integer) of the tile in the canvas
@ -157,10 +182,73 @@ def append_tile_pos(data, geo_extrema, matrixdims):
return ret return ret
def create_playbook(data):
"""
Returns list of dict [delta1, delta2, ...] with changes (in both direections) for every single tour.
The snapshot holds the complete canvas for the actual step. The playbook contains exclusively
changed values (delta).
Rules to process:
- Loop over all n data items: start with the first (oldest) item (0)
- Copy previous snapshot (to find changes in a later step)
- Run recursively over the the snapshot items: Find an existing item at the position
of the actual image and change its position by the displacement rule.
- Place the image of the actual item (a) at its default position in the snapshot
- Create a item for the following image (a+1) and hide it (for scrolling backwards).
- Store all changes betweeen snapshot and previous snapshot in a new delta (a) and append it
to the return dict.
In error case, None will be returned.
"""
snapshot = prev_snapshot = []
ret = []
for a in data:
prev_snapshot = snapshot
(x, y) = (a[F.POSX], a[F.POSY])
snapshot = shifting_snapshot_items(x,y, snapshot)
return ret
def shifting_snapshot_items(x, y, snapshot):
"""
Recursive change item chain starting with item at position x,y.
Returns updated snapshot
"""
goon = True
(actx, acty) = (x, y)
ret = snapshot
while goon:
goon = False
for i, item in enumerate(ret):
if (item[F.POSX], item[F.POSY]) == (actx, acty):
# ret[i][F.POSX], ret[i][F.POSY] =
goon = True
return ret
def shifting(tup, matrixdims):
"""Strategy to shift. Above and on the horizontal middle line, the position
will be shifted to the top. Positions below the horizontal middle line,
the position will be shifted in bottom direction.
Returns the offset as (x, y) tuple, not the new position itself.
"""
def calculate_orig_pos(): def calculate_orig_pos():
global data global data
geo_extrema = find_geo_extrema(data) geo_extrema = find_geo_extrema(data)
data = append_tile_pos(data, geo_extrema, matrixdims) data = append_tile_pos(data, geo_extrema, matrixdims)
playbook = create_playbook(data)
def main(): def main():

12
site_builder/test_find_geo_extrema.py

@ -10,11 +10,11 @@ def patch_log(monkeypatch):
def test_find_geo_extrama_simpple(): def test_find_geo_extrama_simpple():
data = [ data = [
['dummy', 50.1, 8.1 ], [0, 'dummy', 50.1, 8.1 ],
['dummy', 50.2, 8.2 ], [1, 'dummy', 50.2, 8.2 ],
['dummy', 50.3, 8.4 ], [2, 'dummy', 50.3, 8.4 ],
['dummy', 49.3, 8.4 ], [3, 'dummy', 49.3, 8.4 ],
['dummy', 49.3, 7.4 ] [4, 'dummy', 49.3, 7.4 ]
] ]
result = main.find_geo_extrema(data) result = main.find_geo_extrema(data)
@ -24,7 +24,7 @@ def test_find_geo_extrama_simpple():
def test_find_geo_extrama_single(): def test_find_geo_extrama_single():
data = [ data = [
['dummy', 50.1, 8.1 ] [0, 'dummy', 50.1, 8.1 ]
] ]
result = main.find_geo_extrema(data) result = main.find_geo_extrema(data)

6
site_builder/test_read_csv_to_dict.py

@ -18,8 +18,8 @@ def test_read_csv_to_dict_basic(tmp_path):
result = main.read_csv_to_dict(str(file_path)) result = main.read_csv_to_dict(str(file_path))
expected = [ expected = [
['img1', 12.34, 56.78, 'foo', 'bar'], [1, 'img1', 12.34, 56.78, 'foo', 'bar', None, None],
['img2', 90.12, 34.56, 'baz', 'qux'], [2, 'img2', 90.12, 34.56, 'baz', 'qux', None, None],
] ]
assert result == expected assert result == expected
@ -39,7 +39,7 @@ def test_read_csv_to_dict_extra_spaces(tmp_path):
file_path.write_text(content, encoding="utf-8") file_path.write_text(content, encoding="utf-8")
result = main.read_csv_to_dict(str(file_path)) result = main.read_csv_to_dict(str(file_path))
expected = [['img3', 3.23, 4.56, 'foo', 'bar']] expected = [[1, 'img3', 3.23, 4.56, 'foo', 'bar', None, None]]
assert result == expected assert result == expected

Loading…
Cancel
Save