Download Gadm Data -version 3.6- Access

with zipfile.ZipFile(zip_path, 'r') as zip_ref: zip_ref.extractall(output_dir)

# Load the GeoPackage gpkg_path = os.path.join(output_dir, f"gadm36_{country_code}.gpkg") gdf = gpd.read_file(gpkg_path, layer=str(level)) download gadm data -version 3.6-

def generate_feature(gdf, feature_type="boundary"): """ Generate a feature from the GADM data Parameters: - gdf: GeoDataFrame with GADM data - feature_type: Type of feature to generate ('boundary', 'centroid', 'bbox', or 'simplified') """ if gdf is None or len(gdf) == 0: print("No data available") return None features = [] for idx, row in gdf.iterrows(): feature = { "type": "Feature", "properties": {}, "geometry": None } # Add properties for col in gdf.columns: if col != 'geometry': feature["properties"][col] = str(row[col]) if row[col] is not None else None # Generate geometry based on feature_type if feature_type == "boundary": feature["geometry"] = row.geometry.__geo_interface__ elif feature_type == "centroid": centroid = row.geometry.centroid feature["geometry"] = centroid.__geo_interface__ feature["properties"]["feature_type"] = "centroid" elif feature_type == "bbox": bbox = row.geometry.bounds from shapely.geometry import box bbox_geom = box(*bbox) feature["geometry"] = bbox_geom.__geo_interface__ feature["properties"]["feature_type"] = "bounding_box" elif feature_type == "simplified": # Simplify geometry (reduce complexity) simplified = row.geometry.simplify(tolerance=0.01) feature["geometry"] = simplified.__geo_interface__ feature["properties"]["feature_type"] = "simplified" features.append(feature) # Create FeatureCollection feature_collection = { "type": "FeatureCollection", "features": features, "metadata": { "source": "GADM version 3.6", "feature_type": feature_type, "num_features": len(features) } } return feature_collection with zipfile

print(f"Downloading from {url}") response = requests.get(url, stream=True) f"gadm36_{country_code}.gpkg") gdf = gpd.read_file(gpkg_path