Reads vector data from various formats including GeoParquet. This function dynamically determines the file type based on extension and reads it into a GeoDataFrame. It supports both local files and HTTP/HTTPS URLs. Args: source (str): Path to the vector file or URL. lay
(source, layer=None, **kwargs: Any)
| 18923 | |
| 18924 | |
| 18925 | def read_vector(source, layer=None, **kwargs: Any): |
| 18926 | """Reads vector data from various formats including GeoParquet. |
| 18927 | |
| 18928 | This function dynamically determines the file type based on extension |
| 18929 | and reads it into a GeoDataFrame. It supports both local files and HTTP/HTTPS URLs. |
| 18930 | |
| 18931 | Args: |
| 18932 | source (str): Path to the vector file or URL. |
| 18933 | layer (str | int, optional): String or integer specifying which layer to read from multi-layer |
| 18934 | files (only applicable for formats like GPKG, GeoJSON, etc.). |
| 18935 | Defaults to None. |
| 18936 | **kwargs (Any): Additional keyword arguments to pass to the underlying reader. |
| 18937 | |
| 18938 | Returns: |
| 18939 | A GeoDataFrame containing the vector data. |
| 18940 | |
| 18941 | Raises: |
| 18942 | ValueError: If the file format is not supported or source cannot be accessed. |
| 18943 | |
| 18944 | Examples: |
| 18945 | Read a local shapefile |
| 18946 | >>> gdf = read_vector("path/to/data.shp") |
| 18947 | >>> |
| 18948 | Read a GeoParquet file from URL |
| 18949 | >>> gdf = read_vector("https://example.com/data.parquet") |
| 18950 | >>> |
| 18951 | Read a specific layer from a GeoPackage |
| 18952 | >>> gdf = read_vector("path/to/data.gpkg", layer="layer_name") |
| 18953 | """ |
| 18954 | |
| 18955 | import urllib.parse |
| 18956 | |
| 18957 | import fiona |
| 18958 | import geopandas as gpd |
| 18959 | |
| 18960 | # Determine if source is a URL or local file |
| 18961 | parsed_url = urllib.parse.urlparse(source) |
| 18962 | is_url = parsed_url.scheme in ["http", "https"] |
| 18963 | |
| 18964 | # If it's a local file, check if it exists |
| 18965 | if not is_url and not os.path.exists(source): |
| 18966 | raise ValueError(f"File does not exist: {source}") |
| 18967 | elif is_url and source.endswith(".parquet"): |
| 18968 | source = download_file(source, quiet=True, overwrite=True) |
| 18969 | |
| 18970 | # Get file extension |
| 18971 | _, ext = os.path.splitext(source) |
| 18972 | ext = ext.lower() |
| 18973 | |
| 18974 | # Handle GeoParquet files |
| 18975 | if ext in [".parquet", ".pq", ".geoparquet"]: |
| 18976 | return read_parquet(source, **kwargs) |
| 18977 | |
| 18978 | # Handle common vector formats |
| 18979 | if ext in [".shp", ".geojson", ".json", ".gpkg", ".gml", ".kml", ".gpx"]: |
| 18980 | # For formats that might have multiple layers |
| 18981 | if ext in [".gpkg", ".gml"] and layer is not None: |
| 18982 | return gpd.read_file(source, layer=layer, **kwargs) |
no test coverage detected