ダウンロードしたシェープファイルの読み込み

ダウンロードしたシェープファイルの読み込み

今回はオープンデータの鉄道路線を使う
https://nlftp.mlit.go.jp/ksj/gml/datalist/KsjTmplt-N02-v2_3.html

今回は令和元年のデータをダウンロードして使う

Zipをダウンロードして解凍

N02-19_GML
がファイル名になるので

mv ~/Downloads/N02-19_GML .

で移動

今回使用するデータは路線図なので
RailroadSection.shp を使う

gdf = gpd.read_file('./N02-19_GML/N02-19_RailroadSection.shp',encoding='shift-jis')
gdf

で読み込んで表示

	N02_001	N02_002	N02_003	N02_004	geometry
0	23	5	沖縄都市モノレール線	沖縄都市モノレール	LINESTRING (127.67948 26.21454, 127.6797 26.21...
1	12	5	いわて銀河鉄道線	アイジーアールいわて銀河鉄道	LINESTRING (141.29139 40.3374, 141.29176 40.33...
2	12	5	いわて銀河鉄道線	アイジーアールいわて銀河鉄道	LINESTRING (141.27554 40.23936, 141.27567 40.2...
3	12	5	いわて銀河鉄道線	アイジーアールいわて銀河鉄道	LINESTRING (141.28659 40.26092, 141.28538 40.2...
4	12	5	いわて銀河鉄道線	アイジーアールいわて銀河鉄道	LINESTRING (141.29082 40.28615, 141.29089 40.2...
...	...	...	...	...	...
22011	12	4	相鉄新横浜線	相模鉄道	LINESTRING (139.58522 35.48055, 139.58384 35.4...
22012	12	4	相鉄新横浜線	相模鉄道	LINESTRING (139.56671 35.47802, 139.56433 35.4...
22013	11	2	おおさか東線	西日本旅客鉄道	LINESTRING (135.56227 34.68802, 135.564 34.68798)
22014	11	2	東海道線	東日本旅客鉄道	LINESTRING (139.66375 35.57335, 139.66382 35.5...
22015	11	2	東海道線	東日本旅客鉄道	LINESTRING (139.58705 35.4819, 139.58646 35.48...
22016 rows × 5 columns

あとは

gdf.plot()

とすると路線図が表示される

なおgeojsonでも同じ結果になるらしい

一応実験する
しかし
拡張子を .geojsonにしたら

---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
Cell In[10], line 1
----> 1 gdf = gpd.read_file('./N02-19_GML/N02-19_RailroadSection.geojson',encoding='shift-jis')
      2 gdf

File ~/.pyenv/versions/3.11.0/lib/python3.11/site-packages/geopandas/io/file.py:316, in _read_file(filename, bbox, mask, columns, rows, engine, **kwargs)
    313             filename = response.read()
    315 if engine == "pyogrio":
--> 316     return _read_file_pyogrio(
    317         filename, bbox=bbox, mask=mask, columns=columns, rows=rows, **kwargs
    318     )
    320 elif engine == "fiona":
    321     if pd.api.types.is_file_like(filename):

File ~/.pyenv/versions/3.11.0/lib/python3.11/site-packages/geopandas/io/file.py:576, in _read_file_pyogrio(path_or_bytes, bbox, mask, rows, **kwargs)
    567     warnings.warn(
    568         "The 'include_fields' and 'ignore_fields' keywords are deprecated, and "
    569         "will be removed in a future release. You can use the 'columns' keyword "
   (...)    572         stacklevel=3,
    573     )
    574     kwargs["columns"] = kwargs.pop("include_fields")
--> 576 return pyogrio.read_dataframe(path_or_bytes, bbox=bbox, **kwargs)

File ~/.pyenv/versions/3.11.0/lib/python3.11/site-packages/pyogrio/geopandas.py:275, in read_dataframe(path_or_buffer, layer, encoding, columns, read_geometry, force_2d, skip_features, max_features, where, bbox, mask, fids, sql, sql_dialect, fid_as_index, use_arrow, on_invalid, arrow_to_pandas_kwargs, **kwargs)
    270 if not use_arrow:
    271     # For arrow, datetimes are read as is.
    272     # For numpy IO, datetimes are read as string values to preserve timezone info
    273     # as numpy does not directly support timezones.
    274     kwargs["datetime_as_string"] = True
--> 275 result = read_func(
    276     path_or_buffer,
    277     layer=layer,
    278     encoding=encoding,
    279     columns=columns,
    280     read_geometry=read_geometry,
    281     force_2d=gdal_force_2d,
    282     skip_features=skip_features,
    283     max_features=max_features,
    284     where=where,
    285     bbox=bbox,
    286     mask=mask,
    287     fids=fids,
    288     sql=sql,
    289     sql_dialect=sql_dialect,
    290     return_fids=fid_as_index,
    291     **kwargs,
    292 )
    294 if use_arrow:
    295     import pyarrow as pa

File ~/.pyenv/versions/3.11.0/lib/python3.11/site-packages/pyogrio/raw.py:198, in read(path_or_buffer, layer, encoding, columns, read_geometry, force_2d, skip_features, max_features, where, bbox, mask, fids, sql, sql_dialect, return_fids, datetime_as_string, **kwargs)
     59 """Read OGR data source into numpy arrays.
     60 
     61 IMPORTANT: non-linear geometry types (e.g., MultiSurface) are converted
   (...)    194 
    195 """
    196 dataset_kwargs = _preprocess_options_key_value(kwargs) if kwargs else {}
--> 198 return ogr_read(
    199     get_vsi_path_or_buffer(path_or_buffer),
    200     layer=layer,
    201     encoding=encoding,
    202     columns=columns,
    203     read_geometry=read_geometry,
    204     force_2d=force_2d,
    205     skip_features=skip_features,
    206     max_features=max_features or 0,
    207     where=where,
    208     bbox=bbox,
    209     mask=_mask_to_wkb(mask),
    210     fids=fids,
    211     sql=sql,
    212     sql_dialect=sql_dialect,
    213     return_fids=return_fids,
    214     dataset_kwargs=dataset_kwargs,
    215     datetime_as_string=datetime_as_string,
    216 )

File pyogrio/_io.pyx:1344, in pyogrio._io.ogr_read()

File pyogrio/_io.pyx:668, in pyogrio._io.get_fields()

File pyogrio/_ogr.pyx:28, in pyogrio._ogr.get_string()

UnicodeDecodeError: 'shift_jis' codec can't decode byte 0x84 in position 2: illegal multibyte sequence

となった

ファイルが GeoJSON の場合 → 文字コードは 必ず UTF-8 です。
encoding=”shift-jis” を指定すると逆に壊れてしまいます。
N02-19_RailroadSection は国交省の N02 鉄道データですが、配布形式は
* Shapefile (.shp) → 属性が Shift-JIS
* GeoJSON (.geojson) → UTF-8

GeoJSON なら encoding 指定は不要
Shapefile の場合は属性に Shift-JIS が使われている

gdf = gpd.read_file('./N02-19_GML/N02-19_RailroadSection.geojson')
gdf

で成功

次に表示だが小さいと見えにくいので拡大する

fig = plt.figure(figsize=(8,10))
ax = fig.add_subplot(1,1,1)
gdf.plot(ax=ax)

実行すると路線図が拡大表示される