feat(data.py): data-reading file

This commit is contained in:
Mahdi Dibaiee 2019-02-03 09:04:28 +03:30
parent 37d26dba75
commit 902be97332
6 changed files with 183 additions and 2 deletions

93
.gitignore vendored
View File

@ -1 +1,94 @@
geodata
#### joe made this: http://goel.io/joe
#### python ####
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
.hypothesis/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# IPython Notebook
.ipynb_checkpoints
# pyenv
.python-version
# celery beat schedule file
celerybeat-schedule
# dotenv
.env
# virtualenv
.venv/
venv/
ENV/
# Spyder project settings
.spyderproject
# Rope project settings
.ropeproject

View File

@ -1 +1 @@
3.7.2
3.6.8

View File

@ -1,5 +1,5 @@
```
pyenv install 3.7.2
pyenv install $(cat .python-version)
pyenv local
pip install -r requirements.txt
```

74
data.py Normal file
View File

@ -0,0 +1,74 @@
import geopandas
import os
import rasterio
import pandas as pd
from matplotlib import pyplot
from shapely.geometry import Point
directory = os.path.dirname(os.path.abspath(__file__))
GEODATA = os.path.join(directory, 'geodata')
ECOREGIONS = os.path.join(GEODATA, 'ecoregions', 'Ecoregions2017.shp')
ELEVATION = os.path.join(GEODATA, 'srtm', 'topo30-180.tif')
TEMP = os.path.join(GEODATA, 'air_temp')
PRECIP = os.path.join(GEODATA, 'precipitation')
def read_temp_data(year):
return pd.read_csv(os.path.join(TEMP, 'air_temp.{}'.format(year)), sep='\s+', header=None,
names=['longitude', 'latitude', 'january',
'february', 'march', 'april',
'may', 'june', 'july', 'august',
'september', 'november', 'october',
'december', 'yearly_avg'])
def read_precip_data(year):
return pd.read_csv(os.path.join(PRECIP, 'precip.{}'.format(year)), sep='\s+', header=None,
names=['longitude', 'latitude', 'january',
'february', 'march', 'april',
'may', 'june', 'july', 'august',
'september', 'november', 'october',
'december', 'yearly_avg'])
eco = geopandas.read_file(ECOREGIONS)
elevation = rasterio.open(ELEVATION)
# world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
# world['geometry'] = world['geometry'].unary_union
# world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
# print(world.head())
# world = world[['continent', 'geometry']]
# continents = world.dissolve(level=1)
# continents.plot();
# print(eco.head())
# print(elevation)
# eco['geometry'].boundary.plot()
# eco.dissolve()
# eco.plot()
# # rasterio.plot.show(src)
# # pyplot.imshow(elevation.read(1))
# 51.42
# 35.69
# tehran = eco.geometry.contains()
def get_point_information(longitude, latitude):
p = Point(longitude, latitude)
ecoregion = eco.loc[lambda c: c.geometry.contains(p)]
return {
'biome_num': ecoregion.loc['BIOME_NUM'].iloc[0],
'biome_name': ecoregion.loc['BIOME_NAME'].iloc[0],
}
import time
start_time = time.time()
print('Before call')
print('Tehran', get_point_information(51.42, 35.69))
print("--- %s seconds ---" % (time.time() - start_time))
start_time = time.time()
print('Amazon', get_point_information(-59.78, -5.5))
print("--- %s seconds ---" % (time.time() - start_time))
# print(eco.geometry)
# print(tehran.distance(world.boundary))
# world.boundary.plot()
pyplot.show()

13
nn.py Normal file
View File

@ -0,0 +1,13 @@
from __future__ import absolute_import, division, print_function
# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras
# Helper libraries
import numpy as np
import matplotlib.pyplot as plt
import data
print(tf.__version__)

View File

@ -4,3 +4,4 @@ matplotlib==3.0.2
descartes==1.1.0
pysal==2.0.0
rasterio==1.0.15
tensorflow==1.12.0