Upload cgc.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200#
# "let's make something better and understandable,"
# that will always be smaller and less complicated
# everyone can make things bigger, more complicated,
# he who makes things smaller, less complicated and more usefull is a genius
#
# Version : Definitive
#
# Script : canonical grid for a globe creator
#
import numpy as np
import pandas as pd
import duckdb
import sys
import pyarrow as pa
from mpmath import mp
from datetime import datetime
from dataclasses import dataclass
from hilbertcurve.hilbertcurve import HilbertCurve
from typing import Optional, ClassVar
grid_points = 1 # *1e3 on equator
DRC = "c:/"
DRD = "d:/"
DRZ = "z:/"
if sys.platform.strip() != "win32":
DRZ = DRC = DRD = "/"
FOLDER = DRC + "tmp/g/"
mp.dps = 50
@dataclass
class OneDimLoc:
FE: ClassVar[int] = 1e9 # * factor e9 1_000_000_000
x: Optional[int] = None
y: Optional[int] = None
def get_hc(self) -> int:
p = 128
n = 2
hilbert_curve = HilbertCurve(p, n)
return hilbert_curve.distance_from_point([int(self.x), int(self.y)])
def get_tt(self) -> tuple:
p = 128
n = 2
hilbert_curve = HilbertCurve(p, n)
return hilbert_curve.point_from_distance(int(self.x))
def get_tau(self) -> tuple:
ton = (90 - self.x) / 360
if ton > 0.5:
ton = 0
tat = (self.y + 180) / 360
if tat >= 0.99999999972:
tat = 0
return (int(round(ton * self.FE)), int(round(tat * self.FE)))
def get_ton(self) -> int:
ton = (90 - self.x) / 360
if ton > 0.5:
ton = 0
return int(round(ton * self.FE))
def get_tat(self) -> int:
tat = (self.y + 180) / 360
if tat >= 1:
tat = 0
return int(round(tat * self.FE))
def get_lat(self) -> float:
lat = (0.25 - self.x / self.FE) * 360
return lat
def get_lon(self) -> float:
lon = (self.y / self.FE - 0.5) * 360
if lon >= 180:
lon = -180
return lon
def get_dec(self) -> tuple:
lat = (0.25 - self.x / self.FE) * 360
lon = (self.y / self.FE - 0.5) * 360
if lon >= 180:
lon = -180
return (lat, lon)
def wrt(grd):
df = pd.DataFrame(
grd,
columns=["id", "tat", "ton", "lat", "lon"],
)
tp = f"{FOLDER}{grid_points}/{tat:09d}.parquet"
df.to_parquet(tp, index=False, schema=sch, compression="zstd")
ds = f"""
COPY
(SELECT id, tat, ton, lat::DECIMAL(9,7) lat, lon::DECIMAL(10,7) lon
FROM '{tp}')
TO '{tp}' (COMPRESSION 'zstd')
"""
duckdb.execute(ds)
print(tp)
return
sch = pa.schema(
[
("id", pa.int64()),
("tat", pa.uint32()),
("ton", pa.uint32()),
("lat", pa.float64()),
("lon", pa.float64()),
]
)
s = t = 0 # s=switch, t=counter
grid_size = 1 / (grid_points * 1e3) # gridgrootte
grd = [] # init grid list
rnd = 8 # rounding 8 decimals
sql = f"""
SELECT
tat, ton
FROM
'{FOLDER}{grid_points}/*.parquet'
ORDER BY
1 desc, 2 desc
limit 1
"""
# this is done to find the last when the script did not finish
try:
rs = duckdb.execute(sql).fetchone()
ta = rs[0] / 1e9 - grid_size
to = rs[1]
except Exception as e:
# no parquet file
ta = to = 0
grd.append([0, 0, 0, 90, -180])
# tatitude loop
for tat in np.arange(ta + grid_size, 0.50000, grid_size):
ra = round(tat, 18)
if ra > 0.25:
ra = 0.5 - tat
step = mp.mpf((1 / mp.cos((ra - 0.25) * mp.pi * 2)) * grid_size * 1e9)
if step <= 0:
step = to = 1e11
step = mp.mpf(1e9 / mp.ceil(1e9 / step))
tat = round((tat * 1e9))
lc = OneDimLoc(int(tat))
lat = round(lc.get_lat(), rnd)
# tongitude loop
for ton in np.arange(0 + to, 1e9, step):
lc = OneDimLoc(int(tat), int(round(ton * 1)))
lon = round(lc.get_dec()[1], rnd)
id = lc.get_hc()
if to == 0:
grd.append([id, tat, round(ton * 1), lat, lon])
to = 0
t = t + 1
s = s + 1
nu = datetime.now()
if nu.second // 60 == nu.second / 60 and nu.microsecond < 333:
print(grid_points, s / 1e6, nu)
if t == 1e7:
t = 0
wrt(grd) # write the grid to parquet
grd = [] # init grid list
grd.append([95695973385480874, 500000000, 0, -90, -180])
tat = 500000000
wrt(grd)
print("Ready at : ", nu)cgc.pyAdded+200