|
| 1 | +import requests |
| 2 | +import pandas as pd |
| 3 | +import geopandas as gpd |
| 4 | +from shapely.geometry import Point, MultiPoint # Importing Point and MultiPoint classes |
| 5 | + |
| 6 | +''' |
| 7 | +Gir reguleringsgrad i % andel av normal årsavrenning |
| 8 | +''' |
| 9 | + |
| 10 | + |
| 11 | +def qvadis(): |
| 12 | + # URL of the service, WMS |
| 13 | + url = 'https://kart.nve.no/enterprise/rest/services/Mapservices/Qvadis/MapServer/1/query' |
| 14 | + |
| 15 | + # Function to fetch data with pagination |
| 16 | + def fetch_data_with_pagination(url, params, batch_size=1000): |
| 17 | + all_features = [] |
| 18 | + offset = 0 |
| 19 | + while True: |
| 20 | + params['resultOffset'] = offset |
| 21 | + response = requests.get(url, params=params) |
| 22 | + if response.status_code == 200: |
| 23 | + json_data = response.json() |
| 24 | + features = json_data.get('features', []) |
| 25 | + if not features: |
| 26 | + break |
| 27 | + all_features.extend(features) |
| 28 | + if len(features) < batch_size: |
| 29 | + break |
| 30 | + offset += batch_size |
| 31 | + else: |
| 32 | + print("Error:", response.status_code) |
| 33 | + break |
| 34 | + return all_features |
| 35 | + |
| 36 | + # Parameters for the request |
| 37 | + params = { |
| 38 | + 'where': '1=1', # True |
| 39 | + 'text': '', |
| 40 | + 'objectIds': '', |
| 41 | + 'time': '', |
| 42 | + 'timeRelation': 'esriTimeRelationOverlaps', |
| 43 | + 'geometry': '', |
| 44 | + 'geometryType': 'esriGeometryEnvelope', |
| 45 | + 'inSR': '', |
| 46 | + 'spatialRel': 'esriSpatialRelIntersects', |
| 47 | + 'distance': '', |
| 48 | + 'units': 'esriSRUnit_Meters', |
| 49 | + 'relationParam': '', |
| 50 | + 'outFields': '*', #* means returning all columns. YOu can replace this this with a list of columns out want to return. |
| 51 | + 'returnGeometry': True, |
| 52 | + 'returnTrueCurves': False, |
| 53 | + 'maxAllowableOffset': '', |
| 54 | + 'geometryPrecision': '', |
| 55 | + 'outSR': '', |
| 56 | + 'havingClause': '', |
| 57 | + 'returnIdsOnly': False, |
| 58 | + 'returnCountOnly': False, |
| 59 | + 'orderByFields': '', |
| 60 | + 'groupByFieldsForStatistics': '', |
| 61 | + 'outStatistics': '', |
| 62 | + 'returnZ': False, |
| 63 | + 'returnM': False, |
| 64 | + 'gdbVersion': '', |
| 65 | + 'historicMoment': '', |
| 66 | + 'returnDistinctValues': False, |
| 67 | + 'resultOffset': '', |
| 68 | + 'resultRecordCount': 1000, # Set batch size to 1000 |
| 69 | + 'returnExtentOnly': False, |
| 70 | + 'sqlFormat': 'none', |
| 71 | + 'datumTransformation': '', |
| 72 | + 'parameterValues': '', |
| 73 | + 'rangeValues': '', |
| 74 | + 'quantizationParameters': '', |
| 75 | + 'featureEncoding': 'esriDefault', |
| 76 | + 'f': 'pjson' |
| 77 | + } |
| 78 | + |
| 79 | + # Fetch all features using pagination |
| 80 | + all_features = fetch_data_with_pagination(url, params) |
| 81 | + |
| 82 | + # Extracting attributes and geometry coordinates |
| 83 | + attributes = [feature['attributes'] for feature in all_features] |
| 84 | + print(attributes) |
| 85 | + geometry_paths = [feature['geometry']['paths'] for feature in all_features] |
| 86 | + # Extract points from rings for each row |
| 87 | + geometry_points = [MultiPoint([Point(point) for point in path]) for paths in geometry_paths for path in paths] |
| 88 | + print(len(geometry_points)) |
| 89 | + # Create DataFrame |
| 90 | + df = pd.DataFrame(attributes) |
| 91 | + # Create GeoDataFrame |
| 92 | + gdf = gpd.GeoDataFrame(df, geometry=geometry_points) |
| 93 | + |
| 94 | + print(gdf) # Print the GeoDataFrame |
| 95 | + print(gdf.columns) |
| 96 | + |
| 97 | + gdf.to_excel("qvadis.xlsx", index=False) |
| 98 | + |
| 99 | +if __name__ == '__main__': |
| 100 | + qvadis() |
0 commit comments