Skip to content

Commit f6bd01f

Browse files
Kharude, Sachinsackh
Kharude, Sachin
authored andcommitted
removed pandas from dependency and made geobuf optional
Signed-off-by: Kharude, Sachin <sachin.kharude@here.com>
1 parent f56c494 commit f6bd01f

File tree

4 files changed

+51
-52
lines changed

4 files changed

+51
-52
lines changed

docs/source/install.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Installation
22
============
33

44
xyzspaces has optional dependencies for its spatial functionality on a large geospatial, open
5-
source stack of libraries (`Geopandas`_, `turfpy`_). See the
5+
source stack of libraries (`Geopandas`_, `turfpy`_, `geobuf`_). See the
66
:ref:`dependencies` section below for more details. The C depedencies of Geopandas such as (`GEOS`_, `GDAL`_, `PROJ`_)
77
can sometimes be a challenge to install. Therefore, we advise you
88
to closely follow the recommendations below to avoid installation problems.
@@ -116,6 +116,7 @@ Optional depedencies:
116116

117117
- `Geopandas`_
118118
- `turfpy`_
119+
- `geobuf`_
119120

120121
Dev dependencies:
121122

@@ -157,3 +158,5 @@ Dev dependencies:
157158

158159
.. _PROJ: https://proj.org/
159160

161+
.. _geobuf: https://pypi.org/project/geobuf/
162+

requirements.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
backoff>=1.10.0
33
geojson
44
requests
5-
geobuf
65
ijson>=3.1.1
76
pyhocon
87
requests-oauthlib
9-
pandas

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
# Extra dependencies
4141

42-
geo = ["geopandas", "turfpy>=0.0.3"]
42+
geo = ["geopandas", "turfpy>=0.0.3", "geobuf"]
4343

4444
extras_require = {"dev": dev_reqs, "geo": geo}
4545

xyzspaces/spaces.py

Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import concurrent.futures
2929
import copy
30+
import csv
3031
import hashlib
3132
import io
3233
import json
@@ -38,10 +39,8 @@
3839
from multiprocessing import Manager
3940
from typing import TYPE_CHECKING, Any, Dict, Generator, List, Optional, Union
4041

41-
import geobuf
4242
import ijson
43-
import pandas
44-
from geojson import Feature, GeoJSON
43+
from geojson import Feature, FeatureCollection, GeoJSON, Point
4544

4645
from xyzspaces._compact import HAS_GEOPANDAS
4746

@@ -1123,9 +1122,9 @@ def add_features_csv(
11231122
path: str,
11241123
lon_col: str,
11251124
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 = ",",
11291128
add_tags: Optional[List[str]] = None,
11301129
remove_tags: Optional[List[str]] = None,
11311130
features_size: int = 2000,
@@ -1161,49 +1160,47 @@ def add_features_csv(
11611160
:raises Exception: If values of params `lat_col`, `lon_col`, `id_col`
11621161
do not match with column names in csv file.
11631162
"""
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,
11751203
)
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-
)
12071204

12081205
def cluster(
12091206
self,
@@ -1358,6 +1355,7 @@ def add_features_geobuf(
13581355
:param chunk_size: Number of chunks for each process to handle. The default value
13591356
is 1, for a large number of features please use `chunk_size` greater than 1.
13601357
"""
1358+
import geobuf
13611359

13621360
with open(path, "rb") as f:
13631361
geobuff_data = f.read()

0 commit comments

Comments
 (0)