@@ -189,7 +189,7 @@ CREATE or replace FUNCTION osmc.encode_short_code(
189
189
WHERE isolabel_ext = p_isolabel_ext
190
190
AND afa .hBig_to_vbit (cbits) = substring (v .hbitstr FROM 1 FOR l .prefixlen )
191
191
;
192
- $f$ LANGUAGE SQL IMMUTABLE;
192
+ $f$ LANGUAGE SQL IMMUTABLE PARALLEL SAFE ;
193
193
COMMENT ON FUNCTION osmc .encode_short_code (bigint ,text )
194
194
IS ' Computes the short code representation of a hierarchical grid cell for a given jurisdiction.' ;
195
195
@@ -407,7 +407,7 @@ CREATE or replace FUNCTION api.afacode_encode_log_no_context(
407
407
AND g .isolabel_ext LIKE split_part(e .isolabel_ext ,' -' ,1 ) || ' %'
408
408
AND (is_contained IS TRUE OR ST_intersects(e .pt ,g .geom ))
409
409
WHERE g .is_country IS FALSE
410
- $wrap$ LANGUAGE SQL IMMUTABLE;
410
+ $wrap$ LANGUAGE SQL IMMUTABLE PARALLEL SAFE ;
411
411
COMMENT ON FUNCTION api .afacode_encode_log_no_context (text ,int )
412
412
IS ' Encodes a GeoURI into a logistic AFAcode, without requiring prior jurisdictional context.' ;
413
413
@@ -441,9 +441,9 @@ CREATE or replace FUNCTION api.br_afacode_decode_log(
441
441
AND c .isolabel_ext = p_isolabel_ext
442
442
AND cindex = substring (p_code,1 ,1 )
443
443
) j,
444
- LATERAL (SELECT afa .hBig_to_hex (j .hbig ), afa .co_decode (j .hbig ), ((j .hbig )::bit (6 ))::int - 12 ) v(id,geom,id_length),
444
+ LATERAL (SELECT afa .hBig_to_hex (j .hbig ), afa .br_decode (j .hbig ), ((j .hbig )::bit (6 ))::int - 12 ) v(id,geom,id_length),
445
445
LATERAL (SELECT afa .br_cell_area (v .id_length ), afa .br_cell_side (v .id_length )) l(area,side)
446
- $f$ LANGUAGE SQL IMMUTABLE;
446
+ $f$ LANGUAGE SQL IMMUTABLE PARALLEL SAFE ;
447
447
COMMENT ON FUNCTION api .br_afacode_decode_log (text ,text )
448
448
IS ' Decodes a logistic AFAcode for Brazil. Requiring prior jurisdictional context.' ;
449
449
@@ -477,9 +477,9 @@ CREATE or replace FUNCTION api.cm_afacode_decode_log(
477
477
AND c .isolabel_ext = p_isolabel_ext
478
478
AND cindex = substring (p_code,1 ,1 )
479
479
) j,
480
- LATERAL (SELECT afa .hBig_to_hex (j .hbig ), afa .co_decode (j .hbig ), ((j .hbig )::bit (6 ))::int - 12 ) v(id,geom,id_length),
481
- LATERAL (SELECT afa .co_cell_area (v .id_length ), afa .co_cell_side (v .id_length )) l(area,side)
482
- $f$ LANGUAGE SQL IMMUTABLE;
480
+ LATERAL (SELECT afa .hBig_to_hex (j .hbig ), afa .cm_decode (j .hbig ), ((j .hbig )::bit (6 ))::int - 12 ) v(id,geom,id_length),
481
+ LATERAL (SELECT afa .cm_cell_area (v .id_length ), afa .cm_cell_side (v .id_length )) l(area,side)
482
+ $f$ LANGUAGE SQL IMMUTABLE PARALLEL SAFE ;
483
483
COMMENT ON FUNCTION api .cm_afacode_decode_log (text ,text )
484
484
IS ' Decodes a logistic AFAcode for Cameroon. Requiring prior jurisdictional context.' ;
485
485
@@ -513,9 +513,9 @@ CREATE or replace FUNCTION api.co_afacode_decode_log(
513
513
AND c .isolabel_ext = p_isolabel_ext
514
514
AND cindex = substring (p_code,1 ,1 )
515
515
) j,
516
- LATERAL (SELECT afa .hBig_to_hex (j .hbig ), afa .co_decode (j .hbig ), ((j .hbig )::bit (6 ))::int - 12 ) v(id,geom,id_length),
516
+ LATERAL (SELECT afa .hBig_to_hex (j .hbig ), afa .co_decode (j .hbig ), ((j .hbig )::bit (6 ))::int - 12 ) v(id,geom,id_length),
517
517
LATERAL (SELECT afa .co_cell_area (v .id_length ), afa .co_cell_side (v .id_length )) l(area,side)
518
- $f$ LANGUAGE SQL IMMUTABLE;
518
+ $f$ LANGUAGE SQL IMMUTABLE PARALLEL SAFE ;
519
519
COMMENT ON FUNCTION api .co_afacode_decode_log (text ,text )
520
520
IS ' Decodes a logistic AFAcode for Colombia. Requiring prior jurisdictional context.' ;
521
521
@@ -531,7 +531,7 @@ CREATE or replace FUNCTION api.afacode_decode_log(
531
531
END
532
532
FROM regexp_split_to_array(p_code,' ~' ) u,
533
533
LATERAL str_geocodeiso_decode(u[1 ]) l
534
- $wrap$ LANGUAGE SQL IMMUTABLE;
534
+ $wrap$ LANGUAGE SQL IMMUTABLE PARALLEL SAFE ;
535
535
COMMENT ON FUNCTION api .afacode_decode_log (text )
536
536
IS ' Wrapper for country-specific Logistics AFAcode decoders.' ;
537
537
-- EXPLAIN ANALYZE SELECT api.afacode_decode_log('CO-BOY-Tunja~44QZNW');
@@ -540,43 +540,108 @@ COMMENT ON FUNCTION api.afacode_decode_log(text)
540
540
-- ----------------
541
541
-- api jurisdiction coverage:
542
542
543
- CREATE or replace FUNCTION api .jurisdiction_coverage(
544
- p_iso text ,
545
- p_base int DEFAULT 32
543
+ CREATE or replace FUNCTION api .br_jurisdiction_coverage(
544
+ p_iso text
545
+ ) RETURNS jsonb AS $f$
546
+ SELECT
547
+ jsonb_build_object(' type' ,' FeatureCollection' ,' features' ,jsonb_agg(jsonb_build_object(
548
+ ' type' ,' Feature' ,
549
+ ' geometry' ,ST_AsGeoJSON(ST_Transform(c .geom ,4326 ),8 ,0 )::jsonb,
550
+ ' id' , v .id ,
551
+ ' properties' ,jsonb_build_object(
552
+ ' area' , area,
553
+ ' side' , side,
554
+ ' index' , cindex,
555
+ ' is_country' , is_country,
556
+ ' is_contained' , is_contained,
557
+ ' is_overlay' , is_overlay,
558
+ ' level' , id_length
559
+ )
560
+ )))::jsonb
561
+
562
+ FROM osmc .coverage c,
563
+ LATERAL (SELECT afa .hBig_to_hex (c .cbits ), afa .br_decode (c .cbits ), ((c .cbits )::bit (6 ))::int - 12 ) v(id,geom,id_length),
564
+ LATERAL (SELECT afa .br_cell_area (v .id_length ), afa .br_cell_side (v .id_length )) l(area,side)
565
+ WHERE isolabel_ext = p_iso
566
+ $f$ LANGUAGE SQL IMMUTABLE;
567
+ COMMENT ON FUNCTION api .br_jurisdiction_coverage (text )
568
+ IS ' Returns jurisdiction coverage.'
569
+ ;
570
+ -- EXPLAIN ANALYZE SELECT api.br_jurisdiction_coverage('BR-SP-Campinas');
571
+
572
+ CREATE or replace FUNCTION api .cm_jurisdiction_coverage(
573
+ p_iso text
574
+ ) RETURNS jsonb AS $f$
575
+ SELECT
576
+ jsonb_build_object(' type' ,' FeatureCollection' ,' features' ,jsonb_agg(jsonb_build_object(
577
+ ' type' ,' Feature' ,
578
+ ' geometry' ,ST_AsGeoJSON(ST_Transform(c .geom ,4326 ),8 ,0 )::jsonb,
579
+ ' id' , v .id ,
580
+ ' properties' ,jsonb_build_object(
581
+ ' area' , area,
582
+ ' side' , side,
583
+ ' index' , cindex,
584
+ ' is_country' , is_country,
585
+ ' is_contained' , is_contained,
586
+ ' is_overlay' , is_overlay,
587
+ ' level' , id_length
588
+ )
589
+ )))::jsonb
590
+
591
+ FROM osmc .coverage c,
592
+ LATERAL (SELECT afa .hBig_to_hex (c .cbits ), afa .cm_decode (c .cbits ), ((c .cbits )::bit (6 ))::int - 12 ) v(id,geom,id_length),
593
+ LATERAL (SELECT afa .cm_cell_area (v .id_length ), afa .cm_cell_side (v .id_length )) l(area,side)
594
+ WHERE isolabel_ext = p_iso
595
+ $f$ LANGUAGE SQL IMMUTABLE;
596
+ COMMENT ON FUNCTION api .cm_jurisdiction_coverage (text )
597
+ IS ' Returns jurisdiction coverage.'
598
+ ;
599
+
600
+ CREATE or replace FUNCTION api .co_jurisdiction_coverage(
601
+ p_iso text
546
602
) RETURNS jsonb AS $f$
547
- SELECT jsonb_build_object(
548
- ' type' , ' FeatureCollection' ,
549
- ' features' , (coalesce(jsonb_agg(
550
- ST_AsGeoJSONb((ST_Transform(geom,4326 )),7 ,0 ,null ,
551
- jsonb_build_object(
552
- ' code' ,
553
- CASE
554
- WHEN is_country IS FALSE THEN kx_prefix
555
- WHEN p_base IN (16 ,17 ) THEN natcod .vbit_to_baseh (osmc .extract_L0bits (cbits),16 ,true)
556
- WHEN p_base IN (18 ) AND x[2 ] IN (' BR' ) THEN osmc .encode_16h1c (natcod .vbit_to_baseh (osmc .extract_L0bits (cbits),16 ,true),1 )
557
- WHEN p_base IN (18 ) AND x[2 ] IN (' UY' ) THEN osmc .encode_16h1c (natcod .vbit_to_baseh (osmc .extract_L0bits (cbits),16 ,true),4 )
558
- ELSE natcod .vbit_to_strstd (osmc .cbits_16h_to_b32nvu (osmc .extract_L0bits (cbits),osmc .extract_jurisdbits (cbits)),' 32nvu' )
559
- END
560
- ,
561
- ' area' , ST_Area(ggeohash .draw_cell_bybox (bbox,false,ST_SRID(geom))),
562
- ' side' , SQRT(ST_Area(ggeohash .draw_cell_bybox (bbox,false,ST_SRID(geom)))),
563
- ' base' , osmc .string_base (p_base),
564
- ' index' , CASE WHEN is_country IS FALSE THEN cindex ELSE null END,
603
+ SELECT
604
+ jsonb_build_object(' type' ,' FeatureCollection' ,' features' ,jsonb_agg(jsonb_build_object(
605
+ ' type' ,' Feature' ,
606
+ ' geometry' ,ST_AsGeoJSON(ST_Transform(c .geom ,4326 ),8 ,0 )::jsonb,
607
+ ' id' , v .id ,
608
+ ' properties' ,jsonb_build_object(
609
+ ' area' , area,
610
+ ' side' , side,
611
+ ' index' , cindex,
565
612
' is_country' , is_country,
566
613
' is_contained' , is_contained,
567
614
' is_overlay' , is_overlay,
568
- ' level' , length(kx_prefix)
569
- )
570
- )),' []' ::jsonb))
571
- )
572
- FROM osmc .coverage , str_geocodeiso_decode(p_iso) t(x)
573
- WHERE isolabel_ext = x[1 ]
615
+ ' level' , id_length
616
+ )
617
+ )))::jsonb
574
618
619
+ FROM osmc .coverage c,
620
+ LATERAL (SELECT afa .hBig_to_hex (c .cbits ), afa .co_decode (c .cbits ), ((c .cbits )::bit (6 ))::int - 12 ) v(id,geom,id_length),
621
+ LATERAL (SELECT afa .co_cell_area (v .id_length ), afa .co_cell_side (v .id_length )) l(area,side)
622
+ WHERE isolabel_ext = p_iso
575
623
$f$ LANGUAGE SQL IMMUTABLE;
624
+ COMMENT ON FUNCTION api .co_jurisdiction_coverage (text )
625
+ IS ' Returns jurisdiction coverage.'
626
+ ;
627
+
628
+ CREATE or replace FUNCTION api .jurisdiction_coverage(
629
+ p_iso text
630
+ ) RETURNS jsonb AS $wrap$
631
+ SELECT
632
+ CASE l[2 ]
633
+ WHEN ' BR' THEN api .br_jurisdiction_coverage ( l[1 ] )
634
+ WHEN ' CM' THEN api .cm_jurisdiction_coverage ( l[1 ] )
635
+ WHEN ' CO' THEN api .co_jurisdiction_coverage ( l[1 ] )
636
+ ELSE jsonb_build_object(' error' , ' Jurisdiction not supported.' )
637
+ END
638
+ FROM str_geocodeiso_decode(p_iso) l
639
+ $wrap$ LANGUAGE SQL IMMUTABLE PARALLEL SAFE;
576
640
COMMENT ON FUNCTION api .jurisdiction_coverage (text ,int )
577
641
IS ' Returns jurisdiction coverage.'
578
642
;
579
- -- EXPLAIN ANALYZE SELECT api.jurisdiction_coverage('BR-SP-Campinas');
643
+
644
+ -- ----------------
580
645
581
646
-- Add size_shortestprefix in https://github.com/digital-guard/preserv/src/optim-step4-api.sql[api.jurisdiction_geojson_from_isolabel]
582
647
CREATE or replace FUNCTION api .jurisdiction_geojson_from_isolabel(
@@ -587,8 +652,7 @@ CREATE or replace FUNCTION api.jurisdiction_geojson_from_isolabel(
587
652
' features' ,
588
653
(
589
654
jsonb_agg(ST_AsGeoJSONb(
590
- geom,
591
- 8 ,0 ,null ,
655
+ geom,8 ,0 ,null ,
592
656
jsonb_build_object(
593
657
' osm_id' , osm_id,
594
658
' jurisd_base_id' , jurisd_base_id,
0 commit comments