1+ import { TileMatrixSet } from '@basemaps/geo' ;
12import { LogType } from '@basemaps/shared' ;
23import { createWriteStream } from 'fs' ;
3- import { Geometry , LineString , MultiPolygon , Polygon } from 'geojson' ;
4+ import { Feature , Geometry , LineString , MultiPolygon , Polygon } from 'geojson' ;
45import readline from 'readline' ;
56
67import { modifyFeature } from '../modify/modify.js' ;
78import { Metrics , Simplify } from '../schema-loader/schema.js' ;
89import { VectorCreationOptions } from '../stac.js' ;
10+ import { transformNdJson , transformZoom } from '../transform/nztm.js' ;
911import { VectorGeoFeature } from '../types/VectorGeoFeature.js' ;
1012import { createReadStreamSafe } from '../util.js' ;
1113import { Point , simplify } from './simplify.js' ;
@@ -18,6 +20,7 @@ import { Point, simplify } from './simplify.js';
1820export async function generalize (
1921 input : URL ,
2022 output : URL ,
23+ tileMatrix : TileMatrixSet ,
2124 options : VectorCreationOptions ,
2225 logger : LogType ,
2326) : Promise < Metrics > {
@@ -36,21 +39,23 @@ export async function generalize(
3639 let outputCount = 0 ;
3740 for await ( const line of rl ) {
3841 if ( line === '' ) continue ;
42+ const feature = JSON . parse ( line ) as Feature ;
43+ if ( tileMatrix . identifier === 'NZTM2000Quad' ) transformNdJson ( feature ) ;
3944 inputCount ++ ;
4045 // For simplify, duplicate feature for each zoom level with different tolerance
4146 if ( simplify != null ) {
4247 for ( const s of simplify ) {
43- const feature = tag ( options , line , s , logger ) ;
44- if ( feature == null ) continue ;
48+ const vectorGeofeature = tag ( tileMatrix , options , feature , s , logger ) ;
49+ if ( vectorGeofeature == null ) continue ;
4550
46- writeStream . write ( JSON . stringify ( feature ) + '\n' ) ;
51+ writeStream . write ( JSON . stringify ( vectorGeofeature ) + '\n' ) ;
4752 outputCount ++ ;
4853 }
4954 } else {
50- const feature = tag ( options , line , null , logger ) ;
51- if ( feature == null ) continue ;
55+ const vectorGeofeature = tag ( tileMatrix , options , feature , null , logger ) ;
56+ if ( vectorGeofeature == null ) continue ;
5257
53- writeStream . write ( JSON . stringify ( feature ) + '\n' ) ;
58+ writeStream . write ( JSON . stringify ( vectorGeofeature ) + '\n' ) ;
5459 outputCount ++ ;
5560 }
5661 }
@@ -72,13 +77,14 @@ export async function generalize(
7277 * Tag feature for layer
7378 */
7479function tag (
80+ tileMatrix : TileMatrixSet ,
7581 options : VectorCreationOptions ,
76- line : string ,
82+ feature : Feature ,
7783 simplify : Simplify | null ,
7884 logger : LogType ,
7985) : VectorGeoFeature | null {
80- const feature = {
81- ...JSON . parse ( line ) ,
86+ const vectorGeofeature = {
87+ ...structuredClone ( feature ) ,
8288 tippecanoe : {
8389 layer : options . name ,
8490 minzoom : options . layer . style . minZoom ,
@@ -87,35 +93,41 @@ function tag(
8793 } as VectorGeoFeature ;
8894
8995 // copy the stac json's tags to the feature (i.e. 'kind')
90- Object . entries ( options . layer . tags ) . forEach ( ( [ key , value ] ) => ( feature . properties [ key ] = value ) ) ;
91-
92- // adjust the feature's metadata and properties
93- const modifiedFeature = modifyFeature ( feature , options , logger ) ;
94- if ( modifiedFeature == null ) {
95- return null ;
96- }
96+ Object . entries ( options . layer . tags ) . forEach ( ( [ key , value ] ) => ( vectorGeofeature . properties [ key ] = value ) ) ;
9797
9898 // Simplify geometry
9999 if ( simplify != null ) {
100100 // Update the simplified feature zoom level
101- modifiedFeature [ 'tippecanoe' ] = {
101+ vectorGeofeature [ 'tippecanoe' ] = {
102102 layer : options . name ,
103103 minzoom : simplify . style . minZoom ,
104104 maxzoom : simplify . style . maxZoom ,
105105 } ;
106106 if ( simplify . tolerance != null ) {
107- const geom = modifiedFeature . geometry ;
107+ const geom = vectorGeofeature . geometry ;
108108 const type = geom . type ;
109- const coordinates = simplifyFeature ( type , geom , simplify . tolerance ) ;
110- if ( coordinates == null ) {
109+ const geometry = simplifyFeature ( type , geom , simplify . tolerance ) ;
110+ if ( geometry == null ) {
111111 return null ;
112112 }
113- modifiedFeature . geometry = coordinates ;
113+ vectorGeofeature . geometry = geometry ;
114114 }
115115 }
116116
117+ // adjust the feature's metadata and properties
118+ const modifiedFeature = modifyFeature ( vectorGeofeature , options , logger ) ;
119+ if ( modifiedFeature == null ) {
120+ return null ;
121+ }
122+
123+ // Skip features that maxzoom is less than minzoom, this could happened after simplification and special tags on zoom levels
124+ if ( modifiedFeature . tippecanoe . maxzoom < modifiedFeature . tippecanoe . minzoom ) return null ;
125+
126+ // Transform zoom level for NZTM2000Quad
127+ modifiedFeature . tippecanoe . minzoom = transformZoom ( modifiedFeature . tippecanoe . minzoom , tileMatrix ) ;
128+ modifiedFeature . tippecanoe . maxzoom = transformZoom ( modifiedFeature . tippecanoe . maxzoom , tileMatrix ) ;
129+
117130 // Remove unused properties
118- // REVIEW: this function just removes the special tags. something isn't right here
119131 const cleanedFeature = removeAttributes ( modifiedFeature , options ) ;
120132 return cleanedFeature ;
121133}
0 commit comments