# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Django Shell-Plus
#     language: python
#     name: django_extensions
# ---

from ews.models import *
import pandas as pd
from fastkml import  kml

kml_file = "D:/DWC_plattform/rain.kml"
with open(kml_file, 'rt', encoding="utf-8") as myfile:
    doc=myfile.read()

k = kml.KML()

k.from_string(doc)

k

features = list(k.features())

len(features)

list(list(k.features())[0].features())

rain_raw = list(list(k.features())[0].features())

# print out the first 5 stations just as a sanity check
for s in rain_raw[:5]:
    # a type of pygeoif.geometry.Point
    point = s._geometry.geometry
    print(s.name, point.x, point.y)

# +
# lets put them into pandas dataframes
import pandas as pd

rain_list = [{
    'name': s.name,
    'lon': s._geometry.geometry.x,
    'lat': s._geometry.geometry.y
} for s in rain_raw]

rain = pd.DataFrame(rain_list)

# again, let's sanity check we have the right stuff
print(rain.head())


# -

l = rain.name.str.split(' ')

l

rain.name = [item[0] for item in l]

rainvalues = pd.read_csv("D:/DigitalWaterCity/data_processed/rain_siaap.csv", parse_dates = True, index_col = "Date")

rv = rainvalues.groupby("gauge").resample("D").sum()

print(rv.head())

rv = rv.reset_index()

gn = rv.gauge.str.split("_")

rv.gauge= [item[0] for item in gn]

FeatureType.objects.all()

rv.gauge.unique()

rain.name.unique()

f = FeatureType()

f.name = "Rainfall"

f.unit = "mm"

f.save()

rain

for index, row in rain.iterrows():
    loc = Site()
    loc.name = row['name']
    loc.ref_name = row['name']
    loc.geom = {"coordinates": [row['lon'],row['lat']], "type": "Point"}
    loc.feature_type = f
    loc.owner = User.objects.get(id = 1)
    try:
        loc.save()
    except:
        print(loc)

rain



StarcoderdataJupyterScriptsDedupFiltered