|
27 | 27 |
|
28 | 28 | import concurrent.futures
|
29 | 29 | import copy
|
| 30 | +import csv |
30 | 31 | import hashlib
|
31 | 32 | import io
|
32 | 33 | import json
|
|
38 | 39 | from multiprocessing import Manager
|
39 | 40 | from typing import TYPE_CHECKING, Any, Dict, Generator, List, Optional, Union
|
40 | 41 |
|
41 |
| -import geobuf |
42 | 42 | import ijson
|
43 |
| -import pandas |
44 |
| -from geojson import Feature, GeoJSON |
| 43 | +from geojson import Feature, FeatureCollection, GeoJSON, Point |
45 | 44 |
|
46 | 45 | from xyzspaces._compact import HAS_GEOPANDAS
|
47 | 46 |
|
@@ -1123,9 +1122,9 @@ def add_features_csv(
|
1123 | 1122 | path: str,
|
1124 | 1123 | lon_col: str,
|
1125 | 1124 | lat_col: str,
|
1126 |
| - id_col: Optional[str] = "", |
1127 |
| - alt_col: Optional[str] = "", |
1128 |
| - delimiter: Optional[str] = ",", |
| 1125 | + id_col: str = "", |
| 1126 | + alt_col: str = "", |
| 1127 | + delimiter: str = ",", |
1129 | 1128 | add_tags: Optional[List[str]] = None,
|
1130 | 1129 | remove_tags: Optional[List[str]] = None,
|
1131 | 1130 | features_size: int = 2000,
|
@@ -1161,49 +1160,47 @@ def add_features_csv(
|
1161 | 1160 | :raises Exception: If values of params `lat_col`, `lon_col`, `id_col`
|
1162 | 1161 | do not match with column names in csv file.
|
1163 | 1162 | """
|
1164 |
| - df = pandas.read_csv(path, sep=delimiter) |
1165 |
| - key_columns = [lat_col, lon_col] |
1166 |
| - if alt_col: |
1167 |
| - key_columns.append(alt_col) |
1168 |
| - if id_col: |
1169 |
| - key_columns.append(id_col) |
1170 |
| - if not all([col in df.columns for col in key_columns]): |
1171 |
| - raise Exception( |
1172 |
| - "The longitude, latitude coordinates and id column name " |
1173 |
| - "should match with `lon_col`, `lat_col`, " |
1174 |
| - "`id_col` and `alt_col` parameter value" |
| 1163 | + features = [] |
| 1164 | + with open(path) as f: |
| 1165 | + input_file = csv.DictReader(f, delimiter=delimiter) |
| 1166 | + columns = input_file.fieldnames |
| 1167 | + if ( |
| 1168 | + lon_col not in columns # type: ignore |
| 1169 | + or lat_col not in columns # type: ignore |
| 1170 | + or (id_col not in columns if id_col else False) # type: ignore |
| 1171 | + or (alt_col not in columns if alt_col else False) # type: ignore |
| 1172 | + ): |
| 1173 | + raise Exception( |
| 1174 | + "The longitude, latitude coordinates and id column name " |
| 1175 | + "should match with `lon_col`, `lat_col`, " |
| 1176 | + "`id_col` and `alt_col` parameter value" |
| 1177 | + ) |
| 1178 | + for row in input_file: |
| 1179 | + ft = Feature( |
| 1180 | + geometry=Point( |
| 1181 | + ( |
| 1182 | + float(row[lon_col]), |
| 1183 | + float(row[lat_col]), |
| 1184 | + float(row[alt_col]) if alt_col else 0.0, |
| 1185 | + ) |
| 1186 | + ) |
| 1187 | + ) |
| 1188 | + row.pop(lon_col) |
| 1189 | + row.pop(lat_col) |
| 1190 | + row.pop(alt_col, "") |
| 1191 | + ft.properties = row |
| 1192 | + if id_col: |
| 1193 | + ft["id"] = row[id_col] |
| 1194 | + features.append(ft) |
| 1195 | + feature_collection = FeatureCollection(features=features) |
| 1196 | + self.add_features( |
| 1197 | + features=feature_collection, |
| 1198 | + add_tags=add_tags, |
| 1199 | + remove_tags=remove_tags, |
| 1200 | + features_size=features_size, |
| 1201 | + chunk_size=chunk_size, |
| 1202 | + id_properties=id_properties, |
1175 | 1203 | )
|
1176 |
| - geo = df.to_json(orient="records", date_format="iso") |
1177 |
| - json_geo = json.loads(geo) |
1178 |
| - feature_collection: Dict[str, Any] = { |
1179 |
| - "type": "FeatureCollection", |
1180 |
| - "features": [], |
1181 |
| - } |
1182 |
| - for record in json_geo: |
1183 |
| - feature = { |
1184 |
| - "type": "Feature", |
1185 |
| - "geometry": { |
1186 |
| - "type": "Point", |
1187 |
| - "coordinates": [ |
1188 |
| - record[lon_col], |
1189 |
| - record[lat_col], |
1190 |
| - record[alt_col] if alt_col else 0.0, |
1191 |
| - ], |
1192 |
| - }, |
1193 |
| - "properties": {k: v for k, v in record.items() if k not in key_columns}, |
1194 |
| - } |
1195 |
| - if id_col: |
1196 |
| - feature["id"] = record[id_col] |
1197 |
| - feature_collection["features"].append(feature) |
1198 |
| - |
1199 |
| - self.add_features( |
1200 |
| - feature_collection, |
1201 |
| - add_tags=add_tags, |
1202 |
| - remove_tags=remove_tags, |
1203 |
| - features_size=features_size, |
1204 |
| - chunk_size=chunk_size, |
1205 |
| - id_properties=id_properties, |
1206 |
| - ) |
1207 | 1204 |
|
1208 | 1205 | def cluster(
|
1209 | 1206 | self,
|
@@ -1358,6 +1355,7 @@ def add_features_geobuf(
|
1358 | 1355 | :param chunk_size: Number of chunks for each process to handle. The default value
|
1359 | 1356 | is 1, for a large number of features please use `chunk_size` greater than 1.
|
1360 | 1357 | """
|
| 1358 | + import geobuf |
1361 | 1359 |
|
1362 | 1360 | with open(path, "rb") as f:
|
1363 | 1361 | geobuff_data = f.read()
|
|
0 commit comments