Skip to content

Commit

Permalink
baseline
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea Zanin committed Sep 23, 2019
0 parents commit f57adcf
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Adalitix.py

This is the PyPi package to interact with adalitix

## Docs

TODO
1 change: 1 addition & 0 deletions adalitix/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from adalitix.adalitix import Project, Revision, Tiff, addLayer, uploadShapefile
117 changes: 117 additions & 0 deletions adalitix/adalitix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import requests
import json
from PIL import Image
import numpy as np
from os import remove

BASE_URL = "http://localhost:28000"


def uploadShapefile(zipPath, name, revision):
files = {'data': open(zipPath, 'rb')}
values = {'name': name, 'type': "shp", 'revision': revision}

r = requests.post(BASE_URL+"/apis/files",
files=files, params=values)
# TODO: check that response is successful

return r.json()


def addLayer(configPath):
with open(configPath, 'r') as f:
headers = {'content-type': 'application/json'}
r = requests.post(BASE_URL + "/apis/layers",
data=f.read(), headers=headers)
return r.text


class Project:
def __init__(self, name=None, id=None):
if id is None:
if name is None:
raise Exception("Name is required if no id is passed")

r = requests.post(BASE_URL + "/apis/projects",
params={'name': name})

self.id = r.json()["id"]


class Revision:
def __init__(self, name=None, project=None, tags=None, id=None):
if id is None:
if name is None or project is None:
raise Exception(
"Name and project are required if no id is passed")

project_id = project.id if isinstance(
project, Project) else project
values = {'name': name, 'project': project_id}

if tags is not None:
values["tags"] = tags

r = requests.post(BASE_URL + "/apis/revisions",
params=values)

self.id = r.json()["id"]


class Tiff:
def __init__(self, id=None, name=None, revision=None):
if id is None and (name is None or revision is None):
raise Exception(
"Name and revision are required if no id is passed")

self.id = id
self.cached_content = None
self.name = name
self.revision_id = revision.id if isinstance(
revision, Revision) else revision

def content(self):
if self.cached_content is None:
if self.id is None:
raise Exception(
"Cannot read from a file that has not been created yet")

url = BASE_URL + "/geoserver/adalitix/ows?service=WCS&version=2.0.0&request=GetCoverage&coverageId=adalitix:" + id
r = requests.get(url)

self.cached_content = r.content

return self.cached_content

def toNumpyArray(self):
with open("./temporary.tiff", "wb") as f:
f.write(self.content())

im = Image.open("./temporary.tiff")
arr = np.array(im)

remove("./temporary.tiff")

return arr

def saveToFile(self, path):
with open(path, "wb") as f:
f.write(self.content())

def upload(self, tiffPath):
if self.id is None:
files = {'data': open(tiffPath, 'rb')}
values = {'name': self.name, 'type': "tiff",
'revision': self.revision_id}

# TODO: check that response is successful
r = requests.post(BASE_URL+"/apis/files",
files=files, params=values)
self.id = r.json()["id"]
else:
files = {'data': open(tiffPath, 'rb')}

# TODO: check that response is successful
r = requests.post(BASE_URL + "/apis/files/" + self.id, files=files)

return self
15 changes: 15 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from adalitix import Project, Revision, Tiff

new_project = Project("Fruitipy")
new_revision = Revision("v1", new_project)

# Creates a new file
example_file = Tiff(name="meteo_snap",
revision=new_revision)

# connects to an already existing file
# example_file = Tiff(id="0c42aac9-3253-4474-8d56-d25f46fff5ec")

example_file.upload("../data-backup/meteotn/meteotn_201906210000.tiff")
# The second upload overwrites the previous one
example_file.upload("../data-backup/meteotn/meteotn_201906210005.tiff")

0 comments on commit f57adcf

Please sign in to comment.