السؤال
كيف أستورد GeoJSON الذي يحتوي على مصفوفة كائنات متداخلة؟
الإجابة
في هذا الدليل العملي، سنستخدم بيانات مفتوحة متاحة للعامة هنا. ويمكن العثور على نسخة منها هنا.
-
نزّل البيانات بتنسيق GeoJSON وأعد تسمية الملف إلى
geojson.json. -
تعرّف على البنية.
DESCRIBE TABLE file('geojson.json', 'JSON')
┌- أنشئ جدولًا لتخزين صفوف GeoJSON.
المطلوب هنا هو إنشاء صف لكل كائن في مصفوفة features``.
ويشير نوع البيانات المُستنتَج للحقل geometry إلى أنه يقابل MultiPolygon نوع البيانات في ClickHouse.
create table geojson
(
type String,
name String,
crsType String,
crsName String,
featureType String,
id Int64,
inspiredId String,
natCode String,
nameUnit String,
codNut1 String,
codNut2 String,
codNut3 String,
codigoIne String,
shapeLength Float64,
shapeArea Float64,
geometryType String,
geometry MultiPolygon
)
engine = MergeTree
order by id;- حضّر البيانات.
الهدف الرئيسي من الاستعلام هو التحقق من حصولنا على صف واحد لكل كائن في مصفوفة features.
SELECT
type AS type,
name AS name,
crs.type AS crsType,
crs.properties.name AS crsName,
features.type AS featureType,
features.properties.FID AS id,
features.properties.INSPIREID AS inspiredId,
features.properties.NATCODE AS natCode,
features.properties.NAMEUNIT AS nameUnit,
features.properties.CODNUT1 AS codNut1,
features.properties.CODNUT2 AS codNut2,
features.properties.CODNUT3 AS codNut3,
features.properties.CODIGOINE AS codigoIne,
features.properties.SHAPE_Length AS shapeLength,
features.properties.SHAPE_Area AS shapeArea,
features.geometry.type AS geometryType
--,features.geometry.coordinates
FROM file('municipios_ign.geojson', 'JSON')
ARRAY JOIN features
LIMIT 5
┌- أدرِج البيانات.
INSERT INTO geojson
SELECT
type AS type,
name AS name,
crs.type AS crsType,
crs.properties.name AS crsName,
features.type AS featureType,
features.properties.FID AS id,
features.properties.INSPIREID AS inspiredId,
features.properties.NATCODE AS natCode,
features.properties.NAMEUNIT AS nameUnit,
features.properties.CODNUT1 AS codNut1,
features.properties.CODNUT2 AS codNut2,
features.properties.CODNUT3 AS codNut3,
features.properties.CODIGOINE AS codigoIne,
features.properties.SHAPE_Length AS shapeLength,
features.properties.SHAPE_Area AS shapeArea,
features.geometry.type AS geometryType,
features.geometry.coordinates as geometry
FROM file('municipios_ign.geojson', 'JSON')
ARRAY JOIN featuresهنا، يظهر الخطأ التالي:
Code: 53. DB::Exception: Received from localhost:9000. DB::Exception: ARRAY JOIN requires array or map argument. (TYPE_MISMATCH)
Received exception from server (version 24.1.2):يحدث ذلك بسبب تحليل features.geometry.coordinates.
- لنتحقق من نوع البيانات الخاص به.
SELECT DISTINCT toTypeName(features.geometry.coordinates) AS geometry
FROM file('municipios_ign.geojson', 'JSON')
ARRAY JOIN features
┌يمكن تصحيح ذلك بتحويل multipolygon.properties.coordinates إلى Array(Array(Array(Tuple(Float64,Float64)))).
ولتحقيق ذلك، يمكننا استخدام الدالة arrayMap(func,arr1,…).
SELECT distinct
toTypeName(
arrayMap(features.geometry.coordinates->
arrayMap(features.geometry.coordinates->
arrayMap(features.geometry.coordinates-> (features.geometry.coordinates[1],features.geometry.coordinates[2])
,features.geometry.coordinates),
features.geometry.coordinates),
features.geometry.coordinates)
) as toTypeName
FROM file('municipios_ign.geojson', 'JSON')
ARRAY JOIN features;
┌- أدخِل البيانات.
INSERT INTO geojson
SELECT
type as type,
name as name,
crs.type as crsType,
crs.properties.name as crsName,
features.type as featureType,
features.properties.FID id,
features.properties.INSPIREID inspiredId,
features.properties.NATCODE natCode,
features.properties.NAMEUNIT nameUnit,
features.properties.CODNUT1 codNut1,
features.properties.CODNUT2 codNut2,
features.properties.CODNUT3 codNut3,
features.properties.CODIGOINE codigoIne,
features.properties.SHAPE_Length shapeLength,
features.properties.SHAPE_Area shapeArea,
features.geometry.type geometryType,
arrayMap(features.geometry.coordinates->
arrayMap(features.geometry.coordinates->
arrayMap(features.geometry.coordinates-> (features.geometry.coordinates[1],features.geometry.coordinates[2]),features.geometry.coordinates)
,features.geometry.coordinates)
,features.geometry.coordinates) geometry
FROM file('municipios_ign.geojson', 'JSON')
ARRAY JOIN features;SELECT count()
FROM geojson
┌الخلاصة
قد يكون التعامل مع JSON مهمة معقدة. تناول هذا الدليل العملي سيناريو يمكن أن تؤدي فيه مصفوفة كائنات متداخلة إلى زيادة صعوبة هذه المهمة. لأي متطلبات أخرى متعلقة بـ JSON، يُرجى الرجوع إلى الوثائق.