GithubHelp home page GithubHelp logo

xmba15 / gadm Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 0.0 139 KB

A library that provides all countries's national and prefectural boundaries.

License: Other

Python 100.00%
gadm geography geopandas remote-sensing

gadm's Introduction

PyPi PyPi Build Status

๐Ÿ“ GADM


This library provides all countries's national and prefectural boundaries in the form of geopandas dataframe, by dynamically fetching from GADM dataset.

โš™๏ธ Installation


pip install gadm

๐Ÿƒ How to Run


Basic usage


import geopandas as gpd
from gadm import GADMDownloader

downloader = GADMDownloader(version="4.0")

country_name = "Vietnam"
ad_level = 0
gdf = downloader.get_shape_data_by_country_name(country_name=country_name, ad_level=ad_level)

assert isinstance(gdf, gpd.GeoDataFrame)
gdf.plot()

Use with visualization library


GADM's data frame can be interactively visualized on a jupyter notebook with folium

import folium as fl
import geopandas as gpd

from gadm import GADMDownloader

downloader = GADMDownloader(version="4.0")

country_name = "Vietnam"
ad_level = 1
gdf = downloader.get_shape_data_by_country_name(country_name=country_name, ad_level=ad_level)

m = fl.Map(zoom_start=10, tiles="OpenStreetMap")
for _, r in gdf.iterrows():
    sim_geo = gpd.GeoSeries(r["geometry"]).simplify(tolerance=0.001)
    geo_j = sim_geo.to_json()
    geo_j = fl.GeoJson(data=geo_j, style_function=lambda x: {"fillColor": "orange"})
    fl.Popup(r["VARNAME_1"]).add_to(geo_j)
    geo_j.add_to(m)
m

folium map sample

Download satellite images based on boundaries from gadm


Check the following example

๐ŸŽ› Development Environment


conda env create --file environment.yml
conda activate gadm

gadm's People

Contributors

xmba15 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

gadm's Issues

WinError 10060

Hi,

Thanks a lot for your work!

I'm trying to run the following piece and it results in: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

from gadm import GADMDownloader
downloader = GADMDownloader(version="4.0")
ad_level = 2
gdf = downloader.get_shape_data_by_country_name(country_name='Bulgaria', ad_level=ad_level)

Any ideas what went wrong? Thank you!

I'm aslo attaching a full error message:

{
	"name": "AttributeError",
	"message": "",
	"stack": "---------------------------------------------------------------------------
TimeoutError                              Traceback (most recent call last)
File c:\\Users\\Mikhail\\miniconda3\\envs\\havana\\lib\\site-packages\\urllib3\\connection.py:203, in HTTPConnection._new_conn(self)
    202 try:
--> 203     sock = connection.create_connection(
    204         (self._dns_host, self.port),
    205         self.timeout,
    206         source_address=self.source_address,
    207         socket_options=self.socket_options,
    208     )
    209 except socket.gaierror as e:

File c:\\Users\\Mikhail\\miniconda3\\envs\\havana\\lib\\site-packages\\urllib3\\util\\connection.py:85, in create_connection(address, timeout, source_address, socket_options)
     84 try:
---> 85     raise err
     86 finally:
     87     # Break explicitly a reference cycle

File c:\\Users\\Mikhail\\miniconda3\\envs\\havana\\lib\\site-packages\\urllib3\\util\\connection.py:73, in create_connection(address, timeout, source_address, socket_options)
     72     sock.bind(source_address)
---> 73 sock.connect(sa)
     74 # Break explicitly a reference cycle

TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

The above exception was the direct cause of the following exception:

NewConnectionError                        Traceback (most recent call last)
File c:\\Users\\Mikhail\\miniconda3\\envs\\havana\\lib\\site-packages\\urllib3\\connectionpool.py:790, in HTTPConnectionPool.urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, preload_content, decode_content, **response_kw)
    789 # Make the request on the HTTPConnection object
--> 790 response = self._make_request(
    791     conn,
    792     method,
    793     url,
    794     timeout=timeout_obj,
    795     body=body,
    796     headers=headers,
    797     chunked=chunked,
    798     retries=retries,
    799     response_conn=response_conn,
    800     preload_content=preload_content,
    801     decode_content=decode_content,
    802     **response_kw,
    803 )
    805 # Everything went great!

File c:\\Users\\Mikhail\\miniconda3\\envs\\havana\\lib\\site-packages\\urllib3\\connectionpool.py:491, in HTTPConnectionPool._make_request(self, conn, method, url, body, headers, retries, timeout, chunked, response_conn, preload_content, decode_content, enforce_content_length)
    490         new_e = _wrap_proxy_error(new_e, conn.proxy.scheme)
--> 491     raise new_e
    493 # conn.request() calls http.client.*.request, not the method in
    494 # urllib3.request. It also calls makefile (recv) on the socket.

File c:\\Users\\Mikhail\\miniconda3\\envs\\havana\\lib\\site-packages\\urllib3\\connectionpool.py:467, in HTTPConnectionPool._make_request(self, conn, method, url, body, headers, retries, timeout, chunked, response_conn, preload_content, decode_content, enforce_content_length)
    466 try:
--> 467     self._validate_conn(conn)
    468 except (SocketTimeout, BaseSSLError) as e:

File c:\\Users\\Mikhail\\miniconda3\\envs\\havana\\lib\\site-packages\\urllib3\\connectionpool.py:1092, in HTTPSConnectionPool._validate_conn(self, conn)
   1091 if conn.is_closed:
-> 1092     conn.connect()
   1094 if not conn.is_verified:

File c:\\Users\\Mikhail\\miniconda3\\envs\\havana\\lib\\site-packages\\urllib3\\connection.py:611, in HTTPSConnection.connect(self)
    610 sock: socket.socket | ssl.SSLSocket
--> 611 self.sock = sock = self._new_conn()
    612 server_hostname: str = self.host

File c:\\Users\\Mikhail\\miniconda3\\envs\\havana\\lib\\site-packages\\urllib3\\connection.py:218, in HTTPConnection._new_conn(self)
    217 except OSError as e:
--> 218     raise NewConnectionError(
    219         self, f\"Failed to establish a new connection: {e}\"
    220     ) from e
    222 # Audit hooks are only available in Python 3.8+

NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x000002BA26E83B20>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

The above exception was the direct cause of the following exception:

MaxRetryError                             Traceback (most recent call last)
File c:\\Users\\Mikhail\\miniconda3\\envs\\havana\\lib\\site-packages\\requests\\adapters.py:486, in HTTPAdapter.send(self, request, stream, timeout, verify, cert, proxies)
    485 try:
--> 486     resp = conn.urlopen(
    487         method=request.method,
    488         url=url,
    489         body=request.body,
    490         headers=request.headers,
    491         redirect=False,
    492         assert_same_host=False,
    493         preload_content=False,
    494         decode_content=False,
    495         retries=self.max_retries,
    496         timeout=timeout,
    497         chunked=chunked,
    498     )
    500 except (ProtocolError, OSError) as err:

File c:\\Users\\Mikhail\\miniconda3\\envs\\havana\\lib\\site-packages\\urllib3\\connectionpool.py:844, in HTTPConnectionPool.urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, preload_content, decode_content, **response_kw)
    842     new_e = ProtocolError(\"Connection aborted.\", new_e)
--> 844 retries = retries.increment(
    845     method, url, error=new_e, _pool=self, _stacktrace=sys.exc_info()[2]
    846 )
    847 retries.sleep()

File c:\\Users\\Mikhail\\miniconda3\\envs\\havana\\lib\\site-packages\\urllib3\\util\\retry.py:515, in Retry.increment(self, method, url, response, error, _pool, _stacktrace)
    514     reason = error or ResponseError(cause)
--> 515     raise MaxRetryError(_pool, url, reason) from reason  # type: ignore[arg-type]
    517 log.debug(\"Incremented Retry for (url='%s'): %r\", url, new_retry)

MaxRetryError: HTTPSConnectionPool(host='geodata.ucdavis.edu', port=443): Max retries exceeded with url: /gadm/gadm4.0/gpkg/gadm40_BGR.gpkg (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000002BA26E83B20>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond'))

During handling of the above exception, another exception occurred:

ConnectionError                           Traceback (most recent call last)
File c:\\Users\\Mikhail\\miniconda3\\envs\\havana\\lib\\site-packages\\gadm\\gadm_downloader.py:72, in GADMDownloader.get_shape_data_by_country(self, country, ad_level)
     71 try:
---> 72     download_url(
     73         self._url_root_dir + data_path,
     74         abs_data_path,
     75     )
     76 except Exception as e:

File c:\\Users\\Mikhail\\miniconda3\\envs\\havana\\lib\\site-packages\\gadm\\utils.py:23, in download_url(url, file_path)
     14 \"\"\"download file from url
     15 
     16 Parameters
   (...)
     21     local path of the file
     22 \"\"\"
---> 23 res0 = requests.head(url)
     24 assert \"Content-Length\" in res0.headers

File c:\\Users\\Mikhail\\miniconda3\\envs\\havana\\lib\\site-packages\\requests\\api.py:100, in head(url, **kwargs)
     99 kwargs.setdefault(\"allow_redirects\", False)
--> 100 return request(\"head\", url, **kwargs)

File c:\\Users\\Mikhail\\miniconda3\\envs\\havana\\lib\\site-packages\\requests\\api.py:59, in request(method, url, **kwargs)
     58 with sessions.Session() as session:
---> 59     return session.request(method=method, url=url, **kwargs)

File c:\\Users\\Mikhail\\miniconda3\\envs\\havana\\lib\\site-packages\\requests\\sessions.py:589, in Session.request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
    588 send_kwargs.update(settings)
--> 589 resp = self.send(prep, **send_kwargs)
    591 return resp

File c:\\Users\\Mikhail\\miniconda3\\envs\\havana\\lib\\site-packages\\requests\\sessions.py:703, in Session.send(self, request, **kwargs)
    702 # Send the request
--> 703 r = adapter.send(request, **kwargs)
    705 # Total elapsed time of the request (approximately)

File c:\\Users\\Mikhail\\miniconda3\\envs\\havana\\lib\\site-packages\\requests\\adapters.py:519, in HTTPAdapter.send(self, request, stream, timeout, verify, cert, proxies)
    517         raise SSLError(e, request=request)
--> 519     raise ConnectionError(e, request=request)
    521 except ClosedPoolError as e:

ConnectionError: HTTPSConnectionPool(host='geodata.ucdavis.edu', port=443): Max retries exceeded with url: /gadm/gadm4.0/gpkg/gadm40_BGR.gpkg (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000002BA26E83B20>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond'))

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
c:\\Users\\Mikhail\\My Drive\\0 Code\\havana\
otebooks\\tests.ipynb Cell 5 line 5
      <a href='vscode-notebook-cell:/c%3A/Users/Mikhail/My%20Drive/0%20Code/havana/notebooks/tests.ipynb#X11sZmlsZQ%3D%3D?line=1'>2</a> downloader = GADMDownloader(version=\"4.0\")
      <a href='vscode-notebook-cell:/c%3A/Users/Mikhail/My%20Drive/0%20Code/havana/notebooks/tests.ipynb#X11sZmlsZQ%3D%3D?line=3'>4</a> ad_level = 2
----> <a href='vscode-notebook-cell:/c%3A/Users/Mikhail/My%20Drive/0%20Code/havana/notebooks/tests.ipynb#X11sZmlsZQ%3D%3D?line=4'>5</a> gdf = downloader.get_shape_data_by_country_name(country_name='Bulgaria', ad_level=ad_level)

File c:\\Users\\Mikhail\\miniconda3\\envs\\havana\\lib\\site-packages\\gadm\\gadm_downloader.py:110, in GADMDownloader.get_shape_data_by_country_name(self, country_name, ad_level)
    107     self._logger.error(f\"could not find data for {country_name}\")
    108     return None
--> 110 return self.get_shape_data_by_country(country, ad_level)

File c:\\Users\\Mikhail\\miniconda3\\envs\\havana\\lib\\site-packages\\gadm\\gadm_downloader.py:77, in GADMDownloader.get_shape_data_by_country(self, country, ad_level)
     72     download_url(
     73         self._url_root_dir + data_path,
     74         abs_data_path,
     75     )
     76 except Exception as e:
---> 77     self._logger.error(f\"failed to fetch data for {country.common_name} with error {e}\")
     78     return None
     80 layers: List[str] = sorted(fiona.listlayers(abs_data_path))

File c:\\Users\\Mikhail\\miniconda3\\envs\\havana\\lib\\site-packages\\pycountry\\db.py:17, in Data.__getattr__(self, key)
     15 def __getattr__(self, key):
     16     if key not in self._fields:
---> 17         raise AttributeError
     18     return self._fields[key]

AttributeError: "
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.