Pandas と GeoPandas を使用しています
A GeoPandasAdapter
は、 Pandas および GeoPandas ライブラリの操作を容易にするために、 geopandas アダプタのパッケージで提供されています。 でインポート、インスタンス化、および有効化が完了すると Platform
、 HERE Data SDK for Python の多くの読み取りおよび書き込み機能が pd.DataFrame
pd.GeoSeries
gpd.GeoDataFrame
gpd.GeoSeries
、 Python list
および dict
オブジェクトの代わりに、およびを受け入れて返します。
アダプタを有効にしています
HERE GeoPandas アダプタは、次の 3 つの方法のいずれかで適用できます。
- すべての読み取り / 書き込み操作に適用されます
- カタログ単位で提供されます
- 関数呼び出しごとに指定します
以下に、これらの 3 つのオプションを示します。
オブジェクトを介して作成されたすべてのカタログおよび他のエンティティにアダプタを適用するには Platform
Platform
、そのオブジェクトをインスタンス化するときにアダプタを指定します。
from here.platform import Platform
from here.geopandas_adapter import GeoPandasAdapter
platform = Platform(adapter=GeoPandasAdapter())
選択したカタログにのみアダプタを有効にして、対応 get_catalog
する呼び出しで指定することもできます。
from here.platform import Platform
from here.geopandas_adapter import GeoPandasAdapter
platform = Platform()
adapter = GeoPandasAdapter()
weather_eu = platform.get_catalog('hrn:here:data::olp-here:live-weather-eu', adapter=adapter)
weather_na = platform.get_catalog('hrn:here:data::olp-here:live-weather-na', adapter=adapter)
sdii = platform.get_catalog("hrn:here:data::olp-here:olp-sdii-sample-berlin-2")
最後に、アダプターの使用を単一の機能で指定することもできます。
from here.platform import Platform
from here.geopandas_adapter import GeoPandasAdapter
platform = Platform()
adapter = GeoPandasAdapter()
weather_na = platform.get_catalog('hrn:here:data::olp-here:live-weather-na')
live_layer = weather_na.get_layer('latest-data')
weather_df = live_layer.read_partitions([75477, 75648, 75391, 75562], adapter=adapter, record_path="weather_condition_tile")
weather_msgs = live_layer.read_partitions([75477, 75648, 75391, 75562])
DataFrame に読み取ります
バージョン管理、揮発性、インデックス、ストリーム、およびインタラクティブなマップレイヤーからデータおよびメタデータを読み取るには、まずこのユーザー ガイドの対応するセクションで説明されている読み取り関数について理解してください。
get_partitions_metadata
、 read_partitions
、 read_stream_metadata
、 read_stream
、、のすべての標準パラメーター get_features
iter_features
このアダプターおよびそのデータデコーダーに転送されるアダプター固有のパラメーターに加えて、がサポートされます。
データの読み取りおよびデコードを行う場合 、アダプタ固有のパラメータがpd.read_csv
pd.read_parquet
に渡されます。また、各パーティションの実際のデコードを実行する Pandas 関数に似ています。 データに (GEO) DataFrame インデックスがある場合は、それらのインデックスを処理する方法など、単一のパーティションのデコードの詳細を微調整するために使用できます。 は GeoPandasAdapter
、出力を 1 つの DataFrame にまとめます。 サポートされているコンテンツタイプおよび正確なパラメータの詳細については、 GeoPandasDecoder のドキュメントを参照してください。 パーティション名は partition_id
列に保存され、複数のパーティションを一度に読み取るときに、あるパーティションから読み取られたデータと別のパーティションから読み取られたデータを区別します。 partition_id
列の実際の名前は GeoPandasAdapter
、コンストラクタ内で設定できます。また、プロトコルバッファスキーマに続くコンテンツなどの特定の形式のデコードを微調整するための他のパラメータも設定できます。
decode=False
が read_partitions
に渡された場合、または read_stream
デコードが行われない場合、アダプタは使用されず bytes
、を含むプレーンな Python コレクションまたはイテレータが返されます。
パーティションメタデータを取得するには、 get_partitions_metadata を使用します。 GeoPandasAdapter
が有効な場合 pd.DataFrame
list
dict
、またはの代わりに、またはの例に示すようにが返されます。
例 : DataFrame でバージョン管理されたメタデータを取得しています
from here.platform import Platform
from here.geopandas_adapter import GeoPandasAdapter
platform = Platform(adapter=GeoPandasAdapter())
sdii_catalog = platform.get_catalog("hrn:here:data::olp-here:olp-sdii-sample-berlin-2")
versioned_layer = sdii_catalog.get_layer("sample-versioned-layer")
partitions_df = versioned_layer.get_partitions_metadata([377894434, 377894435, 377894440, 377894441])
パーティションメタデータは、インデックス化されていない DataFrame で返されます。
| ID | data_handle | チェックサム | data_size | CRC |
0 | 377894434 | e2eefcae-e695-4f98-8a55-6881ca1ef52d | | 7697 | |
1 | 377894435 | da494218-e5b9-4538-9860-624864a718a7 | | 11963 | |
2 | 377894440 | ef395fe1-51b4-4909-bd3c-3883d88d66b3 | | 569494 | |
3 | 377894441 | a5e1f634-7fbb-43f6-bbdb-7e91edc67879 | | 342066 |
データ を取得およびデコードするには、 read_partitions を使用します。 GeoPandasAdapter
が有効な場合 pd.DataFrame
gpd.GeoDataFrame
list
dict
、またはは、内容に応じて、またはの代わりに、またはが返されます(次の例を参照)。
例 : DataFrame でバージョン管理されたデータを読み取ります
partitions_df = versioned_layer.read_partitions(partition_ids=[377894434, 377894435])
パーティションのデータは、インデックス化されていない DataFrame に返されます。 pd.DataFrame
1 つのまたはのみ gpd.GeoDataFrame
が返されます。 複数のパーティションのデータがすべて同じ出力に含まれます。 partition_id
明確にするために列が追加されます。 列の名前は、レイヤーのコンテンツタイプ、スキーマ、および実際のコンテンツによって異なります。 が partition_ids
指定されていない場合は、レイヤー全体が読み取られます。
この例では 、 Protobuf 形式でエンコードされたコンテンツを読み取ります。
| partition_id | タイル ID | メッセージ | 参照 |
0 | 377894434 | 377894434 | [{'messageId': 'ee7c8af4-fbe0-45e3-9c55-e170f0d2fa64', 'message': {'envelope': {'version': '1.0', 'submitter': 'Probe Ro | [] |
1 | 377894435 | 377894435 | [{'messageId': '4418dfe4-091e-41fe-bb21-49d6524442af', 'message': {'envelope': {'version': '1.0', 'submitter': 'Probe Ro | [] |
( わかりやすいようにテキストが切り捨てられます )
コンテンツタイプおよび実際のスキーマによっては、返された DataFrame を直接使用できる場合と、使用可能なフォームにするために操作をさらに必要とする場合があります。 csv 、 GeoJSON 、 Parquet 、および schemaless のコンテンツタイプはデコードされ、ユーザーに最適な形式に自動的に変換されます。 たとえば 、 GeoJSON はにデコード gpd.GeoDataFrame
されます。 Protobufでエンコードされたデータには、通常、ネストされた複合フィールドや繰り返しフィールド、リスト、辞書、その他の複合データ構造体が含まれます。
GeoPandasDecoder のドキュメント には、デコードを微調整し、すべてのコンテンツタイプ、特に Protobufでエンコードされたデータの結果の出力を改善するために使用できるパラメータが示されています。 record_path
パラメーターは非常に一般的です。指定すると、そのパス内のコンテンツのみがデコードされます。 指定したパスのフィールドが繰り返しフィールドになる場合、パーティションごとに複数の行が返されます。 また、可能な場合は、複数の列に自動的に展開されます。
上記の例を続けて、同じパーティションを再度読み取り、 record_path
パラメーターを指定して、わかりやすいように列を選択します。
columns = ["messageId", "message.envelope.transientVehicleUUID", "message.path.positionEstimate", "metadata.receivedTime"]
messages_df = versioned_layer.read_partitions(partition_ids=[377894434, 377894435], record_path="messages", columns=columns)
結果 :
| partition_id | MessageID | message.envelope.transientVehicleUUID | message.path.positionEstimate | metadata.receivedTime |
0 | 377894434 | ee7c8af4-fbe0-45e3-9c55-e170f0d2fa64 | ee7c8af4-fbe0-45e3-9c55-e170f0d2fa64 | [{'timeStampUTC_ms': '1506403044000', 'positionTyp | 1507151512491 |
1 | 377894434 | eaa76f08-ed02-4893-b524-9bde9296b9f9 | eaa76f08-ed02-4893-b524-9bde9296b9f9 | [{'timeStampUTC_ms': '1506402922000', 'positionTyp | 1507151512491 |
2 | 377894434 | a86fb17f-27a6-4e47-b2fb-77ec61000625 | a86fb17f-27a6-4e47-b2fb-77ec61000625 | [{'timeStampUTC_ms': '1506403015000', 'positionTyp | 1507151512491 |
3 | 377894434 | 79bba846-b804-4026-a980-7d4045e7a493 | 79bba846-b804-4026-a980-7d4045e7a493 | [{'timeStampUTC_ms': '1506403037000', 'positionTyp | 1507151512491 |
4 | 377894434 | cc71d131-e8ed-4269-b1d1-d9c4c3108408 | cc71d131-e8ed-4269-b1d1-d9c4c3108408 | [{'timeStampUTC_ms': '1506402944000', 'positionTyp | 1507151512492 |
( わかりやすいようにテキストと行が切り捨てられます )
partition_id
列は、デコード後に常に自動的に追加されます。
列 message.path.positionEstimate
には一覧表が含まれています。この一覧表はさらに処理でき、 DataFrame がメッセージごとに 1 つの行を持つことを想定して位置ごとに 1 つの行に変換します。
from here.geopandas_adapter.utils.dataframe import unpack_columns
estimates_df = messages_df[["messageId", "message.path.positionEstimate"]].explode("message.path.positionEstimate")
estimates_df = unpack_columns(estimates_df, "message.path.positionEstimate", keep_prefix=False)
結果 :
| MessageID | timeStampUTC_ms | positionType | 経度 _ 度 | 緯度 _ 度 | horizontalAccuracy_m | Heading_Deg | speed_mps | mapMatchedLinkID | mapMatchedLinkIDOffset_m |
0 | ee7c8af4-fbe0-45e3-9c55-e170f0d2fa64 | 1506403044000 | RAW_GPS | 13.3611 | 52.5099 | 0 | 90.8589 | 16 | 175536727 | 0 |
0 | ee7c8af4-fbe0-45e3-9c55-e170f0d2fa64 | 1506403046000 | RAW_GPS | 13.3616 | 52.5099 | 0 | 91.4001 | 16 | 175536727 | 32 |
0 | ee7c8af4-fbe0-45e3-9c55-e170f0d2fa64 | 1506403048000 | RAW_GPS | 13.3621 | 52.5098 | 0 | 91.5694 | 16 | 175536727 | 64 |
0 | ee7c8af4-fbe0-45e3-9c55-e170f0d2fa64 | 1506403050000 | RAW_GPS | 13.3625 | 52.5098 | 0 | 91.5694 | 16 | 175536727 | 92.1063 |
1 | eaa76f08-ed02-4893-b524-9bde9296b9f9 | 1506402922000 | RAW_GPS | 13.3731 | 52.5092 | 0 | 85.7321 | 16 | 180105322 | 0 |
( わかりやすいように列と行が切り捨てられます )
パーティションメタデータを取得するには、 get_partitions_metadata を使用します。 GeoPandasAdapter
が有効な場合 pd.DataFrame
list
dict
、またはの代わりに、またはの例に示すようにが返されます。
例 : DataFrame で揮発性メタデータを取得しています
from here.platform import Platform
from here.geopandas_adapter import GeoPandasAdapter
platform = Platform(adapter=GeoPandasAdapter())
weather_catalog = platform.get_catalog('hrn:here:data::olp-here:live-weather-eu')
volatile_layer = weather_catalog.get_layer('latest-data')
partitions_df = volatile_layer.get_partitions_metadata(partition_ids=[81150, 81151])
パーティションメタデータは、インデックス化されていない DataFrame で返されます。
| ID | data_handle | チェックサム | data_size | CRC |
0 | 81150 | 81150 | | | |
1 | 81151 | 81151 | | |
データ を取得およびデコードするには、 read_partitions を使用します。 GeoPandasAdapter
が有効な場合 pd.DataFrame
gpd.GeoDataFrame
list
dict
、またはは、内容に応じて、またはの代わりに、またはが返されます(次の例を参照)。
注
揮発性のメタデータおよび基になるデータが同期していないことがあります。 この状況が発生すると、メタデータは、データが特定のパーティションに存在するが、現在の時点ではそこにデータが存在しないことを示している場合があります。 を呼び出し read_partitions
たときに、 1 つ以上の要求されたパーティションが存在しないか、またはデータが含まれていない場合、そのパーティションの返された DataFrame に行は追加されません。 この結果、空の DataFrame が返される可能性があります。
例 : DataFrame 内の揮発性データの読み取り
partitions_df = volatile_layer.read_partitions(partition_ids=[81150, 81151], record_path="weather_condition_tile")
パーティションのデータは、インデックス化されていない DataFrame に返されます。 pd.DataFrame
1 つのまたはのみ gpd.GeoDataFrame
が返されます。 複数のパーティションのデータがすべて同じ出力に含まれます。 partition_id
明確にするために列が追加されます。 列の名前は、レイヤーのコンテンツタイプ、スキーマ、および実際のコンテンツによって異なります。 が partition_ids
指定されていない場合は、レイヤー全体が読み取られます。
この例では 、 Protobuf 形式でエンコードされたコンテンツを読み取ります。
columns = ["tile_id",
"center_point_geohash",
"air_temperature.value",
"dew_point_temperature.value",
"humidity.value",
"air_pressure.value",
"visibility.value",
"iop.value",
"wind_velocity.value",
"wind_velocity.direction",
"precipitation_type.precipitation_type"]
partitions_df = volatile_layer.read_partitions(partition_ids=[81150, 81151], record_path="weather_condition_tile", columns=columns)
この例では 、 Protobuf Repeated フィールドから取得した列のみを選択 weather_condition_tile
します。その結果、次のようになります。
| partition_id | tile_id | center_point_geohash | air_temperature.value | dew_point_temperature.value | humidity.value | air_pressure.value | visibility.value | iop.value | wind_velocity.value | wind_velocity.direction | precipitation_type.precipitation_type |
0 | 81150 | 332391761 | g7ybnf00 | 4.83 | 2 | 82.09 | 1003.09 | 9.99 | 0 | 33.5 | 22.81 | なし |
1 | 81150 | 332391760 | g7ybn600 | 4.84 | 2 | 82.04 | 1003.08 | 9.99 | 0 | 33.47 | 22.73 | なし |
2 | 81150 | 332391767 | g7ybpy00 | 4.8 | 2 | 82.26 | 1003.12 | 9.99 | 0 | 33.62 | 23.09 | なし |
3 | 81150 | 332391765 | g7ybpf00 | 4.81 | 2 | 82.18 | 1003.11 | 9.99 | 0 | 33.57 | 22.97 | なし |
4 | 81150 | 332391764 | g7ybp600 | 4.82 | 2 | 82.14 | 1003.1 | 9.99 | 0 | 33.53 | 22.89 | なし |
( わかりやすいように行が切り捨てられます )
パーティションメタデータを取得するには、 get_partitions_metadata を使用します。 GeoPandasAdapter
が有効な場合 pd.DataFrame
list
dict
、またはの代わりに、またはの例に示すようにが返されます。
例 : DataFrame のインデックスメタデータを取得しています
from here.platform import Platform
from here.geopandas_adapter import GeoPandasAdapter
platform = Platform(adapter=GeoPandasAdapter())
sdii_catalog = platform.get_catalog("hrn:here:data::olp-here:olp-sdii-sample-berlin-2")
index_layer = sdii_catalog.get_layer("sample-index-layer")
partitions_df = index_layer.get_partitions_metadata(query="hour_from=ge=10")
パーティションメタデータは、インデックス化されていない DataFrame で返されます。 インデックス レイヤーにはパーティションの適切な識別子がないため、データ ハンドルはパーティション ID の代わりに使用されます。
| ID | data_handle | チェックサム | data_size | CRC |
0 | 1d63cfb6-5b79-455a-8fda-1503b99253e3 | 1d63cfb6-5b79-455a-8fda-1503b99253e3 | 0353f45622ac843cabbc8af4ce6739d5baf171a | 290391 | |
1 | 1f9c8d0a-2519-4cd8-af4a-0fd0fa16b047 | 1f9c8d0a-2519-4cd8-af4a-0fd0fa16b047 | 1a1472a4de647291da7498407b59a2011af6c25c | 113261 | |
2 | 2f9c978d-b6bc-4889-b7d4-a47849fb6a17 | 2f9c978d-b6bc-4889-b7d4-a47849fb6a17 | 74b94f931c3bda3a7500eadaf34506445c0a10ba | 356674 | |
3 | 2fed9456-7275-4786-b600-0c4865854b79 | 2fed9456-7275-4786-b600-0c4865854b79 | ad68c63881bfeae3635d64270df4e132049f54 | 115175 | |
4 | 3b0c053b-8988-4621-92d7-9daf65e7d4a7 | 3b0c053b-8988-4621-92d7-9daf65e7d4a7 | e7aca6afb0a37ed46d9e11a8c2ed73afa9eae1d0 です | 114945 |
データ を取得およびデコードするには、 read_partitions を使用します。 GeoPandasAdapter
が有効な場合 pd.DataFrame
gpd.GeoDataFrame
list
dict
、またはは、内容に応じて、またはの代わりに、またはが返されます(次の例を参照)。 が partition_ids
指定されていない場合は、レイヤー全体が読み取られます。
例 : DataFrame 内のインデックスデータの読み取り
partitions_df = index_layer.read_partitions(query="hour_from=ge=10")
パーティションのデータは、インデックス化されていない DataFrame に返されます。 pd.DataFrame
1 つのまたはのみ gpd.GeoDataFrame
が返されます。 複数のパーティションのデータがすべて同じ出力に含まれます。 partition_id
明確にするために列が追加されます。 列の名前は、レイヤーのコンテンツタイプ、スキーマ、および実際のコンテンツによって異なります。 インデックス レイヤーにはパーティションの適切な識別子がないため、データ ハンドルはパーティション ID の代わりに使用されます。
| partition_id | 封筒 | パス | pathEvents | pathMedia |
0 | 1d63cfb6-5b79-455a-8fda-1503b99253e3 | {'version': '1.0', 'submitter': 'Probe Route Simul | {'positionEstimate': array([{'timeStampUTC_ms': 15 | {'vehicleStatus': None, 'vehicleDynamics': None, ' | |
1 | 1d63cfb6-5b79-455a-8fda-1503b99253e3 | {'version': '1.0', 'submitter': 'Probe Route Simul | {'positionEstimate': array([{'timeStampUTC_ms': 15 | {'vehicleStatus': None, 'vehicleDynamics': None, ' | |
2 | 1d63cfb6-5b79-455a-8fda-1503b99253e3 | {'version': '1.0', 'submitter': 'Probe Route Simul | {'positionEstimate': array([{'timeStampUTC_ms': 15 | {'vehicleStatus': None, 'vehicleDynamics': None, ' | |
3 | 1d63cfb6-5b79-455a-8fda-1503b99253e3 | {'version': '1.0', 'submitter': 'Probe Route Simul | {'positionEstimate': array([{'timeStampUTC_ms': 15 | {'vehicleStatus': None, 'vehicleDynamics': None, ' | |
4 | 1d63cfb6-5b79-455a-8fda-1503b99253e3 | {'version': '1.0', 'submitter': 'Probe Route Simul | {'positionEstimate': array([{'timeStampUTC_ms': 15 | {'vehicleStatus': None, 'vehicleDynamics': None, ' |
( わかりやすいようにテキストと行が切り捨てられます )
この特定の例では、他の種類のレイヤーについて説明し、 「 DataFrames および GeoDataFrames の操作」のセクションで詳しく説明しているように、 unpack_columns
この関数を使用して辞書を適切な列にさらに展開すると便利です。
from here.geopandas_adapter.utils import dataframe
columns = ["partition_id", "pathEvents"]
events_df = dataframe.unpack_columns(partitions_df[columns], ["pathEvents"], keep_prefix=False)
結果 :
| partition_id | vehicleStatus | vehicleDynamics | signRecognition | laneBoundaryRecognition | exceptionalVehicleState | proprietaryInfo | environmentStatus |
0 | 1d63cfb6-5b79-455a-8fda-1503b99253e3 | | | [{'timeStampUTC_ms': 1506402914000, 'positionOffse | | | | |
1 | 1d63cfb6-5b79-455a-8fda-1503b99253e3 | | | [{'timeStampUTC_ms': 1506403395000, 'positionOffse | | | | |
2 | 1d63cfb6-5b79-455a-8fda-1503b99253e3 | | | [{'timeStampUTC_ms': 1506403082000, 'positionOffse | | | | |
3 | 1d63cfb6-5b79-455a-8fda-1503b99253e3 | | | なし | | | | |
4 | 1d63cfb6-5b79-455a-8fda-1503b99253e3 | | | [{'timeStampUTC_ms': 1506403131000, 'positionOffse | | | |
( わかりやすいように、テキスト、列、行が切り捨てられます )
get_stream_metadata を使用 して、ストリームサブスクリプションからパーティションメタデータを消費します。 GeoPandasAdapter
が有効な場合 pd.DataFrame
list
dict
、またはの代わりに、またはの例に示すようにが返されます。
例 : DataFrame でのストリームメタデータの取得
from here.platform import Platform
from here.geopandas_adapter import GeoPandasAdapter
platform = Platform(adapter=GeoPandasAdapter())
sdii_catalog = platform.get_catalog("hrn:here:data::olp-here:olp-sdii-sample-berlin-2")
stream_layer = sdii_catalog.get_layer("sample-streaming-layer")
with stream_layer.subscribe() as subscription:
partitions_df = stream_layer.get_stream_metadata(subscription=subscription)
パーティションメタデータ(ストリームメッセージ)は、インデックス化されていない DataFrame で返されます。 この例のようにデータにインライン化することも、大きすぎる場合は Blob API を介して保存することもできます。
| ID | data_handle | data_size | データ | チェックサム | CRC | タイムスタンプ | kafka_partition | kafka_offset |
0 | c755c5f5-3e01-4398-a3cd-f9a99393b5b4 | | | b'\n\x031.0\x12\x15 プローブルートサイマル A | c5b9d6040e7cb1ca805f20e26e3c5e3f818d3cc59b9f637c443b9b7b90018fa0 | | 2021-11-26 14:00:52.695000 | 3 | 18856435 |
1 | b69f5967-1408-44d9-9f2a-6e6fd4ec274a | | | b'\n\x031.0\x12\x15 プローブルートサイマル A | bff2e955dff1d35c0a52916aafce8200ebf876c8055204b56d688929fae4ff70 | | 2021-11-26 14:00:57.833000 | 3 | 18856436 |
2 | 14eb5324-1c3b-44dc-8632-47cfa1dc051e | | | b'\n\x031.0\x12\x15 プローブルートサイマル A | 2463cf999a2d97d991 adef6af957ed34a3902a1619b3b6f447c4f61c2dd162b6 | | 2021-11-26 14:01:01.933000 | 3 | 18856437 |
3 | 03c70b04-1f15-46a2-8745-15793cac4eb5 | | | b'\n\x031.0\x12\x15 プローブルートサイマル A | ee4432e0d4a6d527ab4c1ea38d61672172b30dd90598f3f9b7d082a601f3ab を指定します | | 2021-11-26 14:01:05.037000 | 3 | 18856438 |
4 | 2ba84d9e-a4fd-44b5-980b-8db2f04d80b6 | | | b'\n\x031.0\x12\x15 プローブルートサイマル A | be4406f678f4ae882fe85e153f62ebab55270772dea094eae49a11358c6dd222 | | 2021-11-26 14:01:11.253000 です | 3 | 18856439 |
( わかりやすいようにテキストと行が切り捨てられます )
ストリームサブスクリプションからデータを消費、取得、およびデコードするには、 read_stream を使用します。 GeoPandasAdapter
が有効な場合 pd.DataFrame
gpd.GeoDataFrame
list
dict
、またはは、内容に応じて、またはの代わりに、またはが返されます(次の例を参照)。
例 : DataFrame 内のストリームデータの読み取り
この例では、などのアダプタ固有のパラメータ record_path
を使用してデコードをカスタマイズする方法を示します。 データのプロパティの選択のみに関心があります。
この例では 、 Protobuf 形式でエンコードされたコンテンツを読み取ります。
with stream_layer.subscribe() as subscription:
columns = ["timeStampUTC_ms",
"latitude_deg",
"longitude_deg",
"heading_deg",
"speed_mps"]
partitions_df = stream_layer.read_stream(subscription=subscription, record_path="path.positionEstimate", columns=columns)
パーティションのデータは、インデックス化されていない DataFrame に返されます。 pd.DataFrame
1 つのまたはのみ gpd.GeoDataFrame
が返されます。 複数のパーティションのデータがすべて同じ出力に含まれます。 partition_id
明確にするために列が追加されます。 列の名前は、レイヤーのコンテンツタイプ、スキーマ、および実際のコンテンツによって異なります。
| partition_id | PARTITION_TIMESTAMP | timeStampUTC_ms | 緯度 _ 度 | 経度 _ 度 | Heading_Deg | speed_mps |
0 | ae93f978-777a-4afe-AB08-993162ef934a. | 2021-11-26 13:56:18.727000 | 1637934814720 | 52.5263 | 13.3499 | 276.471 | 16 |
1 | ae93f978-777a-4afe-AB08-993162ef934a. | 2021-11-26 13:56:18.727000 | 1637934816720 | 52.5263 | 13.3496 | 268.154 | 16 |
2 | ae93f978-777a-4afe-AB08-993162ef934a. | 2021-11-26 13:56:18.727000 | 1637934818720 | 52.5263 | 13.3491 | 268.179 | 16 |
3 | ae93f978-777a-4afe-AB08-993162ef934a. | 2021-11-26 13:56:18.727000 | 1637934820720 | 52.5263 | 13.3486 | 268.946 | 16 |
4 | ae93f978-777a-4afe-AB08-993162ef934a. | 2021-11-26 13:56:18.727000 | 1637934822720 | 52.5263 | 13.3482 | 269.345 | 16 |
( わかりやすいように行が切り捨てられます )
GeoDataFrame の Interactive マップ レイヤーから機能を取得します
search_features を使用 して、 1 つのインタラクティブマップ レイヤーからフィーチャを取得します。 GeoPandasAdapter
が有効な場合 gpd.GeoDataFrame
list
dict
、またはの代わりに、またはの例に示すようにが返されます。
このレイヤーは、他の関数をサポートしています。この関数の中には、レイヤーからフィーチャーを照会して取得する get_features および spatrical_search があります。 GeoDataFrame は、これらの関数からも返されます。
Jupyter ノートブックで実行して いる場合、 GeoDataFrame を使用すると、地図上で簡単に機能を視覚的に確認できます。以下の例では、 HERE インスペクタを使用して説明しています。
例 : GeoDataFrame のフィーチャの読み取り
この例では、サンプルカタログとサンプルのインタラクティブなマップ レイヤーからベルリンの地区( Bezirk )を取得します。
from here.platform import Platform
from here.geopandas_adapter import GeoPandasAdapter
platform = Platform(adapter=GeoPandasAdapter())
sample_catalog = platform.get_catalog("hrn:here:data::olp-here:here-geojson-samples")
iml_layer = sample_catalog.get_layer("berlin-interactivemap")
features_gdf = iml_layer.search_features()
search_features
パラメーターを指定しない場合、すべての内容が返され、次の結果になります。
| 形状 | Bez | BezName | @ns:com:here:xyz |
pjB2hRwTpsW2ZAoP | MULTIPOLYGON Z (((13.429401 52.508571 0, 13.429028 | 01 | Mitte | {'createdAt': 1629098476655, 'updatedAt': 1629098476655} |
bzuUAjSSniAlAza3 | MULTIPOLYGON Z (((13.491453 52.488265 0, 13.490708 | 02 | Friedrichshain-Kreuzberg | {'createdAt': 1629098476655, 'updatedAt': 1629098476655} |
p6PdohLKy98613Yh | MULTIPOLYGON Z (((13.523023 52.645034 0, 13.522967 | 03 | Pankow | {'createdAt': 1629098476655, 'updatedAt': 1629098476655} |
rBPLWN1rBqpn3e48 | MULTIPOLYGON Z (((13.34142 52.504867 0, 13.341344 | 04 | Charlottenburg-Wilmersdorf | {'createdAt': 1629098476655, 'updatedAt': 1629098476655} |
Jawrgifeu6bFL4SE | MULTIPOLYGON Z (((13.282182 52.53405 0, 13.282092 | 05 | Spandau | {'createdAt': 1629098476655, 'updatedAt': 1629098476655} |
( わかりやすいようにテキストと行が切り捨てられます )
次のように、検索パラメータを指定することもできます。
features_gdf = iml_layer.search_features(params={"p.BezName": "Pankow"}, force_2d=True)
1 つの領域のみを選択し、座標から z レベルを削除します。
| 形状 | Bez | BezName | @ns:com:here:xyz |
p6PdohLKy98613Yh | MULTIPOLYGON (((13.523023 52.645034, 13.522967 52. | 03 | Pankow | {'createdAt': 1629098476655, 'updatedAt': 1629098476655} |
( わかりやすいようにテキストが切り捨てられます )
結果は、 Jupyter ノートブックで実行して いるときに、 HERE インスペクタを使用してマップ上に直接レンダリングできます。
from here.inspector import inspect
from here.inspector.styles import Color
inspect(features_gdf, "Districts of Berlin", style=Color.BLUE)
例 : GeoDataFrame 内の機能の地理空間検索
この例では、ベルリンの区を、街のランドマークである Zoologischer Garten 駅から 1000m の距離にあるクエリで検索します。この駅は、クエリで表示されている座標にあります。
from here.platform import Platform
from here.geopandas_adapter import GeoPandasAdapter
platform = Platform(adapter=GeoPandasAdapter())
sample_catalog = platform.get_catalog("hrn:here:data::olp-here:here-geojson-samples")
iml_layer = sample_catalog.get_layer("berlin-interactivemap")
features_gdf = iml_layer.spatial_search(lng=13.33474, lat=52.50686, radius=1000)
結果 :
| 形状 | Bez | BezName | @ns:com:here:xyz |
pjB2hRwTpsW2ZAoP | MULTIPOLYGON Z (((13.429401 52.508571 0, 13.429028 | 01 | Mitte | {'createdAt': 1629098476655, 'updatedAt': 1629098476655} |
rBPLWN1rBqpn3e48 | MULTIPOLYGON Z (((13.34142 52.504867 0, 13.341344 | 04 | Charlottenburg-Wilmersdorf | {'createdAt': 1629098476655, 'updatedAt': 1629098476655} |
jLrIE0BxQ6vj5U2a | MULTIPOLYGON Z (((13.427455 52.38578 0, 13.426965 | 07 | Tempelhof-Schöneberg | {'createdAt': 1629098476655, 'updatedAt': 1629098476655} |
結果は、次のものを使用して Jupyter ノートブックで直接レンダリングできます。
from here.inspector import inspect
from here.inspector.styles import Color
inspect(features_gdf, "Districts within 1000m from Berlin Zoologischer Garten railway station", style=Color.RED)
DataFrame をレイヤーに書き込みます
バージョン管理、揮発性、インデックス、ストリーム、およびインタラクティブなマップの各レイヤーにデータとメタデータを書き込むには、まずこのユーザー ガイドの対応するセクションで説明されている書き込み関数について理解してください。
GeoPandas アダプタでサポートされているコンテンツタイプ ( 表を参照) の場合、 DataFrame または GeoDataFrame のコンテンツをエンコードして、単一の関数でレイヤーに書き込むことができます。 サポートされていないコンテンツタイプの場合 encode=False
は、自分でエンコードを渡して処理する必要があります。
set_partitions_metadata
、 write_partitions
、 append_stream_metadata
、 write_stream
、、のすべての標準パラメーター write_features
update_features
は delete_features
、このアダプタおよびそのデータエンコーダに転送されるアダプタ固有のパラメータに加えて、サポートされています。
データの書き込みおよびエンコードを行う場合、は GeoPandasAdapter
(GE) DataFrame を分割 partition_id
し、列に従ってパーティションに書き込みます。 行の選択内容は、それぞれエンコードされ、スタンドアロンのパーティションとして保存されます。 パーティション識別子が設定されていない行は廃棄されます。 アダプタ固有のパラメータ DataFrame.to_csv
DataFrame.to_parquet
は、に渡され、各パーティションの実際の符号化を実行する同様の関数になります。 これらの機能を使用して、 (GE) DataFrame インデックスの処理方法など、単一パーティションのエンコードの詳細を微調整できます。 サポートされているコンテンツタイプと正確なパラメータの詳細については、 GeoPandasEncoder のドキュメントを参照してください。
encode=False
write_partitions
がまたはに渡された場合 write_stream
bytes
は、 (GE) DataFrame を含む ( ジオ ) プレーンの Python コレクションも渡す必要があります。これは、アダプタが使用されず、符号化も行われないためです。
書き込みの例は、上に示した読み取りの例と対称です。
DataFrames と GeoDataFrames を操作します
一般的に使用されている Pandas ライブラリおよび GeoPandas ライブラリについては、文書化されており、データの分析および操作を行うための使用方法を示す多くの例が公開されています。 一般に、データは表形式で表示され、テーブルの各セルには、定義されたデータ型 ( 数値、文字列、またはその他の基本型 ) を持つ 1 つの値が含まれます。
マップ データおよび一般に、カタログに保存されているデータは高度に構造化され、複雑なネストされたスキーマをたどることがあります。 Pandas ではこのような複雑な問題に対処 することが困難な場合があります。 したがって、 HERE Data SDK for Python には 、繰り返し 作業を実行し、複雑な DataFrame を操作する HERE -geopandas アダプタパッケージユーティリティ関数が含まれています。特に、単一の値ではなく、ディクショナリを含む列を持つ DataFrame が含まれています。
Series および DataFrame の開梱
Pandas には 、list
列に含まれているタイプのオブジェクトを複数の行に変換するための展開機能があります。 同様 dict
に、 HERE Data SDK for Python には、複数の列を含む単一の列を回転させる unpack_columns 関数と unpack_columns 関数があります。 これは、カタログからデータを読み込んだり、複雑なデータモデルを操作したりすることによって生じる可能性があるデータ構造を展開するための便利な機能です。
unpack
Series
が含まれている dict
オブジェクトに適用 DataFrame
され、を返します。 unpack_columns
DataFrame
dict
オブジェクトを含む 1 つ以上の列を、ディクショナリの各フィールドに 1 つずつ、複数の列に置き換えるためににに適用されます。 また、完全にネストされたデータ構造を簡単に処理できるように、展開も再帰的に行われます。
例 : ディクショナリが含まれている DataFrame 列を展開します
df
構造化オブジェクトから派生した DataFrame の例を次に示します。
import pandas as pd
berlin = {
"name": "Berlin",
"location": {
"longitude": 13.408333,
"latitude": 52.518611,
"country": { "name": "Deutschland", "code": "DE" }
},
"zip_codes": { "min": 10115, "max": 14199 },
"population": 3664088
}
paris = {
"name": "Paris",
"location": {
"longitude": 2.351667,
"latitude": 48.856667,
"country": { "name": "France", "code": "FR" }
},
"zip_codes": { "min": 75001, "max": 75020 },
"population": 2175601
}
df = pd.DataFrame([berlin, paris])
結果 :
| 名前 | 位置情報 | zip_codes | 人口 |
0 | ベルリン | {'longitude': 13.408333, 'latitude': 52.518611, 'country': {'name': 'Deutschland', 'code': 'DE'}} | {'min': 10115, 'max': 14199} | 3664088 |
1 | パリ | {'longitude': 2.351667, 'latitude': 48.856667, 'country': {'name': 'France', 'code': 'FR'}} | {'min': 75001, 'max': 75020} | 2175601 |
列を展開して location
zip_codes
、操作が困難な辞書を含めることができます。 展開処理は再帰的で、たとえば country
に含まれているネストされたディクショナリも展開さ location
れます。
from here.geopandas_adapter.utils.dataframe import unpack_columns
unpacked_df = unpack_columns(df, columns=["location", "zip_codes"])
結果 :
| 名前 | location.longitude | location.latitude | location.country.name | location.country.code | zip_codes.min | zip_codes.max | 人口 |
0 | ベルリン | 13.4083 | 52.5186 | Deutschland | DE | 10115 | 14199 | 3664088 |
1 | パリ | 2.35167 | 48.8567 | フランス | FR | 75001 | 75020 | 2175601 |
列を 1 つ以上の列で置き換えます
関数 replace_column を使用 DataFrame
すると、の 1 つの列を別の DataFrame の 1 つ以上の列に置き換えることができます。
例 : 1 つの列を複数の列に置き換える
例の DataFrames と df
df2
を指定します。
import pandas as pd
df = pd.DataFrame({
"col_A": [11, 31, 41],
"col_B": [12, 32, 42],
"col_C": [14, 34, 42]
}, index = [1, 3, 4])
df2 = pd.DataFrame({
"col_Bx": [110, 130, 140],
"col_By": [115, 135, 145]
}, index = [1, 3, 4])
結果 :
| col_A | col_B | col_C |
1 | 11 | 12 | 14 |
3 | 31 | 32 | 34 |
4 | 41 | 42 | 42 |
および :
| col_Bx | col_By |
1 | 110 | 115 |
3 | 130 | 135 |
4 | 140 | 145 |
col_B
col_Bx
AND に置き換えることができます col_By
。
from here.geopandas_adapter.utils.dataframe import replace_column
replaced_df = replace_column(df, "col_B", df2)
結果 :
| col_A | col_Bx | col_By | col_C |
1 | 11 | 110 | 115 | 14 |
3 | 31 | 130 | 135 | 34 |
4 | 41 | 140 | 145 | 42 |
列名へのプレフィックスの追加と削除
PREFIX_COLUMNS 関数および UNPREFIX_COLUMNSDataFrame
関数は、の選択した列の名前にプレフィックスを追加または削除するために使用されます。 .
接頭辞と列名の間に区切り文字が追加されます。
これは、 DataFrame の関連する列を共通の接頭辞でグループ化 ( 接頭辞 ) する場合、または複数の列 (接頭辞なし) に存在する長い冗長な接頭辞を削除して、操作しやすい派生 DataFrame を取得する場合に役立ちます。
例 : 共通の接頭辞を持つ列の接頭辞
DataFrame の例を示し df
ます。
import pandas as pd
df = pd.DataFrame({
"name": ["Sarah", "Vivek", "Marco"],
"age": [41, 29, 35],
"house_nr": ["1492", "34-35", "48A"],
"road": ["SE 36th Ave", "Seshadri Road", "Via Giosuè Carducci"],
"city": ["Portland", "Bengaluru", "Milan"],
"zip": [97214, 560009, 20123],
"state": ["OR", "KA", pd.NA],
"country": ["US", "IN", "IT"],
})
結果 :
| 名前 | age | house_nr | 道路 | city | 郵便番号 | 都道府県 | 国 |
0 | Sarah | 41 | 1492 | SE 36th Ave | ポートランド | 97214 | または | 米国 |
1 | Vivek | 29 | 34-35 | Seshadri Road | Bengaluru | 560009 | KA | インチ |
2 | Marco | 35 | 48A | Via Giosuè Carducci | Milan | 20123 | | IT |
住所の一部である列をグループ化して、次の文字列を前に付けることができます address
。
from here.geopandas_adapter.utils.dataframe import prefix_columns
prefixed_df = prefix_columns(df, "address", ["house_nr", "road", "city", "zip", "country", "state"])
結果 :
| 名前 | age | address.house_nr | address.road | address.city | address.zip | address.state | address.country |
0 | Sarah | 41 | 1492 | SE 36th Ave | ポートランド | 97214 | または | 米国 |
1 | Vivek | 29 | 34-35 | Seshadri Road | Bengaluru | 560009 | KA | インチ |
2 | Marco | 35 | 48A | Via Giosuè Carducci | Milan | 20123 | | IT |
例 : 共通のプレフィックスを削除します
上記の例を続行 address
すると、プレフィックスを削除して元の DataFrame を取得できます。
from here.geopandas_adapter.utils.dataframe import unprefix_columns
unprefixed_df = unprefix_columns(prefixed_df, "address")
結果 :
| 名前 | age | house_nr | 道路 | city | 郵便番号 | 都道府県 | 国 |
0 | Sarah | 41 | 1492 | SE 36th Ave | ポートランド | 97214 | または | 米国 |
1 | Vivek | 29 | 34-35 | Seshadri Road | Bengaluru | 560009 | KA | インチ |
2 | Marco | 35 | 48A | Via Giosuè Carducci | Milan | 20123 | | IT |