Download scraper-scripts/ExtractAndArchiveLayers.py from CashlessConsumer/chennai-floods-raw: direct link, hf CLI and curl.
- Browser
- Download file 1.57 kB
-
https://huggingface.co/datasets/CashlessConsumer/chennai-floods-raw/resolve/main/scraper-scripts/ExtractAndArchiveLayers.py
- Command line
-
hf download hf://datasets/CashlessConsumer/chennai-floods-raw/scraper-scripts/ExtractAndArchiveLayers.py
-
curl -L -o ExtractAndArchiveLayers.py https://huggingface.co/datasets/CashlessConsumer/chennai-floods-raw/resolve/main/scraper-scripts/ExtractAndArchiveLayers.py
1.57 kB
| import csv | |
| import requests | |
| from owslib.wfs import WebFeatureService | |
| from urllib.parse import urlencode | |
| # GeoServer base URL | |
| geoserver_url = 'https://www.chennaifloodsdss.in/geohorr/ChennaiDSS/ows' | |
| # Function to fetch and store layer names into a CSV file | |
| def store_layers_to_csv(): | |
| try: | |
| wfs = WebFeatureService(geoserver_url, version='2.0.0') | |
| layers = list(wfs.contents) | |
| with open('dump/layers.csv', 'w', newline='') as csvfile: | |
| writer = csv.writer(csvfile) | |
| writer.writerow(layers) | |
| print('Layer names stored in layers.csv') | |
| except Exception as e: | |
| print('Error storing layer names:', e) | |
| # Function to download data for each layer | |
| def download_layer_data(layers): | |
| try: | |
| for layer in layers: | |
| layer_url = f'{geoserver_url}?service=WFS&request=GetFeature&typeName={layer}&outputFormat=application/json' | |
| response = requests.get(layer_url) | |
| with open(f'dump/{layer}.json', 'wb') as file: | |
| file.write(response.content) | |
| print(f"Downloaded data for layer '{layer}'") | |
| except Exception as e: | |
| print(f"Error downloading data for layer '{layer}':", e) | |
| # Execute both functions | |
| def run(): | |
| try: | |
| store_layers_to_csv() | |
| with open('dump/layers.csv', newline='') as csvfile: | |
| reader = csv.reader(csvfile) | |
| layers = next(reader) | |
| download_layer_data(layers) | |
| except Exception as e: | |
| print('Error:', e) | |
| run() | |