我正在嘗試使用 Python Polars 讀取 GeoJSON,如下所示:
import polars as pl
myfile = '{"type":"GeometryCollection","geometries":[{"type":"Linestring","coordinates":[[10,11.2],[10.5,11.9]]},{"type":"Point","coordinates":[10,20]}]}'
pl.read_json(myfile)
我得到的錯誤是:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "...\local-packages\Python39\site-packages\polars\functions.py", line 631, in read_json return DataFrame.read_json(source) # type: ignore
File "...\local-packages\Python39\site-packages\polars\frame.py", line 346, in read_json
self._df = PyDataFrame.read_json(file)
RuntimeError: Other("Error(\"missing field `columns`\", line: 1, column: 143)")
我也嘗試將相同的內容放入檔案中,但我遇到了類似的錯誤。
正如GitHub 中所建議的,我嘗試通過 Pandas 讀取檔案,如下所示:
import pandas as pd
initial_df = pl.from_pandas(pd.read_json(file_path))
我得到的錯誤是:
File "...\file_splitter.py", line 13, in split_file
initial_df = pl.from_pandas(pd.read_json(file_path))
File "...\local-packages\Python39\site-packages\polars\functions.py", line 566, in from_pandas
data[name] = _from_pandas_helper(s)
File "...\local-packages\Python39\site-packages\polars\functions.py", line 534, in _from_pandas_helper
return pa.array(a)
File "pyarrow\array.pxi", line 302, in pyarrow.lib.array
File "pyarrow\array.pxi", line 83, in pyarrow.lib._ndarray_to_array
File "pyarrow\error.pxi", line 97, in pyarrow.lib.check_status
pyarrow.lib.ArrowInvalid: cannot mix list and non-list, non-null values
如何讀取 GeoJSON 檔案?
uj5u.com熱心網友回復:
如果你用 pandas 讀取檔案,你會得到Object一個不知道的型別的列Arrow(它可能是任何東西)。
如果我們將列轉換為字串型別,我們知道箭頭和極坐標可以處理它。
myfile = '{"type":"GeometryCollection","geometries":[{"type":"Linestring","coordinates":[[10,11.2],[10.5,11.9]]},{"type":"Point","coordinates":[10,20]}]}'
print(pl.from_pandas(pd.read_json(myfile).astype(str)))
shape: (2, 2)
┌────────────────────┬─────────────────────────────────────┐
│ type ┆ geometries │
│ --- ┆ --- │
│ str ┆ str │
╞════════════════════╪═════════════════════════════════════╡
│ GeometryCollection ┆ {'type': 'Linestring', 'coordina... │
├????????????????????┼?????????????????????????????????????┤
│ GeometryCollection ┆ {'type': 'Point', 'coordinates':... │
└────────────────────┴─────────────────────────────────────┘
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/422875.html
標籤:
