├── _version.py ├── setup.py ├── README.md ├── LICENSE ├── main.py ├── helpers.py └── .gitignore /_version.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | 3 | __version__ = '0.0.1' -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup 2 | import os 3 | 4 | here = os.path.dirname(os.path.abspath(__file__)) 5 | version_ns = {} 6 | with open(os.path.join(here, '_version.py')) as f: 7 | exec(f.read(), {}, version_ns) 8 | 9 | setup( 10 | name='gee-extraction', 11 | version=version_ns['__version__'], 12 | py_modules=['gee-extraction'], 13 | install_requires=[ 14 | 'Click', 15 | 'pyshp', 16 | 'pycrs' 17 | ], 18 | entry_points=''' 19 | [console_scripts] 20 | gee-extraction=main:main 21 | ''', 22 | ) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gee-extraction 2 | ## Extract Features from Google Earth Engine Scripts 3 | 4 | Google Earth Engine's code editor brings you the possibility to create Features (Points, Lines, Polygons) and Feature Collections, which can even be a mix of Points, Lines and Polygons, and save it "in the code". But, sometimes you need to download those Features to your local drive, to use it with your preferred local software. 5 | 6 | This code aims to make that process easy and automatic. 7 | 8 | This code will be pure python, using simple libraries for file managements and [pyshp](https://github.com/GeospatialPython/pyshp) for exporting to shapefiles format. 9 | 10 | WIP -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Rodrigo E. Principe 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import click 2 | import os 3 | import helpers 4 | import shapefile as pyshp 5 | 6 | 7 | @click.group() 8 | def main(): 9 | pass 10 | 11 | 12 | @main.command() 13 | @click.argument('fn') 14 | @click.argument('result_fn') 15 | def points2shape(fn, result_fn): 16 | """ Extract points from a GEE's code editor script and save them into a shapefile 17 | 18 | Parameters: 19 | 20 | - FN: Input filename. Must be a JavaScript code coming from GEE's code editor 21 | 22 | - RESULT_FN: The resulting filename for the shapefile 23 | """ 24 | fpath = os.path.join(os.getcwd(), fn) 25 | with open(fpath, 'r') as thefile: 26 | content = thefile.read() 27 | multipoints = helpers.extract_color_multipoints(content) 28 | with pyshp.Writer(result_fn, 8) as writeshp: 29 | writeshp.field('Name', 'C', size=50) 30 | writeshp.field('Color', 'C', size=10) 31 | for data in multipoints: 32 | # print(data) 33 | writeshp.record(data['name'], data['color']) 34 | writeshp.multipoint(data['coords']) 35 | 36 | # PRJ 37 | helpers.write_prj(result_fn) 38 | return result_fn 39 | -------------------------------------------------------------------------------- /helpers.py: -------------------------------------------------------------------------------- 1 | """ Helper functions """ 2 | import os.path 3 | import re 4 | import pycrs 5 | 6 | WGS84 = """ 7 | GEOGCS[ 8 | "GCS_WGS_1984", 9 | DATUM[ 10 | "D_WGS_1984", 11 | SPHEROID["WGS_1984",6378137,298.257223563] 12 | ], 13 | PRIMEM["Greenwich",0], 14 | UNIT["Degree",0.017453292519943295] 15 | ] 16 | """ 17 | 18 | 19 | def write_prj(fn, datum=WGS84): 20 | """ Write the .prj file """ 21 | crs = pycrs.parse.from_esri_wkt(datum) 22 | base = os.path.basename(fn) 23 | filename = base.split('.')[0] 24 | folder = os.path.dirname(fn) 25 | prj = os.path.join(folder, '{}.prj'.format(filename)) 26 | with open(prj, "w") as writer: 27 | writer.write(crs.to_esri_wkt()) 28 | return prj 29 | 30 | def extract_color(text): 31 | """ Extract color from parsed text """ 32 | pattern = '\#[\d\w]+' 33 | return re.search(pattern, text).group() 34 | 35 | 36 | def convert_multipoint_coords(text): 37 | """ convert multipoint coords in a string into a list of points """ 38 | pattern = '(\d+\.\d+)|(-\d+\.\d+)' 39 | allcords = re.findall(pattern, text) 40 | final = [] 41 | for i in range(0, len(allcords)-1, 2): 42 | coord0 = allcords[i] 43 | coord0 = coord0[0] if coord0[0] else coord0[1] 44 | coord1 = allcords[i+1] 45 | coord1 = coord1[0] if coord1[0] else coord1[1] 46 | final.append([float(coord0), float(coord1)]) 47 | return final 48 | 49 | 50 | def extract_color_multipoints(text): 51 | """ Extract points from text """ 52 | color_pattern = "(\/\*\s*color:\s*#\w+\s*\*\/)" 53 | with_color = "(\s+)(\w+)(\s*=\s*){}ee.Geometry.MultiPoint\(([\s\S]+?)\)".format(color_pattern) 54 | 55 | match = re.findall(with_color, text) 56 | 57 | result = [] 58 | for m in match: 59 | midres = dict() 60 | midres['name'] = m[1] 61 | midres['color'] = extract_color(m[3]) 62 | coords = m[4].replace('\n', '').replace(' ', '') 63 | midres['coords'] = convert_multipoint_coords(coords) 64 | result.append(midres) 65 | 66 | return result -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | 131 | # IntelliJ 132 | .idea/ --------------------------------------------------------------------------------