-
Notifications
You must be signed in to change notification settings - Fork 50
/
example.py
45 lines (35 loc) · 1.06 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""
Generates `example.parquet` using pyarrow by running `python example.py`.
You can print the metadata with:
.. code-block:: python
>>> import json, pprint, pyarrow.parquet as pq
>>> pprint.pprint(json.loads(pq.read_schema("example.parquet").metadata[b"geo"]))
"""
import json
import pathlib
import geopandas
import pyarrow as pa
import pyarrow.parquet as pq
HERE = pathlib.Path(__file__).parent
df = geopandas.read_file(geopandas.datasets.get_path("naturalearth_lowres"))
df = df.to_crs("crs84")
table = pa.Table.from_pandas(df.head().to_wkb())
metadata = {
"version": "0.5.0-dev",
"primary_column": "geometry",
"columns": {
"geometry": {
"encoding": "WKB",
"geometry_types": ["Polygon", "MultiPolygon"],
"crs": json.loads(df.crs.to_json()),
"edges": "planar",
"bbox": [round(x, 4) for x in df.total_bounds],
},
},
}
schema = (
table.schema
.with_metadata({"geo": json.dumps(metadata)})
)
table = table.cast(schema)
pq.write_table(table, HERE / "example.parquet")