Commit 16246ca9 authored by Muhamad's avatar Muhamad

Tiles Downloader

parent c132fea6
Pipeline #166 canceled with stages
{
"cells": [
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import json\n",
"import psycopg2\n",
"from config import config\n",
"from downtiles import *\n",
"\n",
"def get_tiles(provinsi='', tabel=''):\n",
" try:\n",
" filedownload = f\"{provinsi}/download/\"\n",
" fileresult = f\"{provinsi}/result/\"\n",
" fileoutput = f\"{provinsi}/output/\"\n",
" os.makedirs(os.path.dirname(filedownload), exist_ok=True)\n",
" os.makedirs(os.path.dirname(fileresult), exist_ok=True)\n",
" os.makedirs(os.path.dirname(fileoutput), exist_ok=True)\n",
" params = config()\n",
" conn = psycopg2.connect(**params)\n",
" cur = conn.cursor()\n",
" cur.execute(\"select ST_AsGeoJSON(geom :: geometry) from %s where name_1 ILIKE '%s'\" % (tabel, provinsi))\n",
" poly_prov = cur.fetchall()\n",
" poly = json.loads(poly_prov[0][0])\n",
" latlat = []\n",
" lnglng = []\n",
" for x in poly['coordinates']:\n",
" for lng, lat in x[0]:\n",
" latlat.append(lat)\n",
" lnglng.append(lng)\n",
" lat_max = max(float(sub) for sub in latlat)\n",
" lat_min = min(float(sub) for sub in latlat)\n",
" lng_max = max(float(sub) for sub in lnglng)\n",
" lng_min = min(float(sub) for sub in lnglng)\n",
"\n",
"\n",
" down_tiles(provinsi, lng_min, lng_max, lat_min, lat_max)\n",
" \n",
" # close communication with the PostgreSQL database server\n",
" cur.close()\n",
" # commit the changes\n",
" conn.commit()\n",
" except (Exception, psycopg2.DatabaseError) as error:\n",
" print(error)\n",
" finally:\n",
" if conn is not None:\n",
" conn.close()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"206506,135568\n",
"206506,135569\n",
"206506,135570\n",
"206506,135571\n",
"206506,135572\n",
"206507,135568\n"
]
},
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-8-4e5b095ba0ae>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mget_tiles\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mprovinsi\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'lampung'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtabel\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'geo_kota1'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;32m<ipython-input-7-d3bf7565b051>\u001b[0m in \u001b[0;36mget_tiles\u001b[1;34m(provinsi, tabel)\u001b[0m\n\u001b[0;32m 31\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 32\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 33\u001b[1;33m \u001b[0mdown_tiles\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mprovinsi\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlng_min\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlng_max\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlat_min\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlat_max\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 34\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 35\u001b[0m \u001b[1;31m# close communication with the PostgreSQL database server\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mE:\\data NA\\downloader_tiles\\downtiles.py\u001b[0m in \u001b[0;36mdown_tiles\u001b[1;34m(nama_dir, long_min, long_max, lat_min, lat_max)\u001b[0m\n\u001b[0;32m 138\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0my\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfloor\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0my_min\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mfloor\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0my_max\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 139\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34mf\"{x},{y}\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 140\u001b[1;33m \u001b[0mpng_path\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdownload_tile\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mzoom\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtile_server\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 141\u001b[0m \u001b[0mgeoreference_raster_tile\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mzoom\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mpng_path\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 142\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mE:\\data NA\\downloader_tiles\\downtiles.py\u001b[0m in \u001b[0;36mdownload_tile\u001b[1;34m(x, y, z, tile_server)\u001b[0m\n\u001b[0;32m 80\u001b[0m \"{z}\", str(z))\n\u001b[0;32m 81\u001b[0m \u001b[0mpath\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34mf'{temp_dir}/{x}_{y}_{z}.png'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 82\u001b[1;33m \u001b[0murllib\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrequest\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0murlretrieve\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0murl\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mpath\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 83\u001b[0m \u001b[1;32mreturn\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpath\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 84\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mc:\\users\\owi-khansia\\appdata\\local\\programs\\python\\python37\\lib\\urllib\\request.py\u001b[0m in \u001b[0;36murlretrieve\u001b[1;34m(url, filename, reporthook, data)\u001b[0m\n\u001b[0;32m 255\u001b[0m \u001b[1;31m# Handle temporary file setup.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 256\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mfilename\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 257\u001b[1;33m \u001b[0mtfp\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfilename\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'wb'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 258\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 259\u001b[0m \u001b[0mtfp\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtempfile\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mNamedTemporaryFile\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdelete\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mFalse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mKeyboardInterrupt\u001b[0m: "
]
}
],
"source": [
"get_tiles(provinsi='lampung', tabel='geo_kota1')"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"^C\n"
]
}
],
"source": [
"!python download.py --provinsi=jawa_barat --db=NationalAdd --host=localhost --user=admin --password=admin123 --namatabel=geo_kota1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.8"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment