@@ -188,8 +188,10 @@ def transform_geom(transformer, g, precision=-1):
188188 help = "Indentation level for JSON output" )
189189@click .option ('--projected/--geographic' , default = False ,
190190 help = "Output in projected coordinates (default is geographic)." )
191+ @click .option ('--json-seq/--json-obj' , default = False ,
192+ help = "Write a JSON sequence (default is object)." )
191193@click .pass_context
192- def shapes (ctx , src_path , precision , indent , projected ):
194+ def shapes (ctx , src_path , precision , indent , projected , json_seq ):
193195 verbosity = ctx .obj ['verbosity' ]
194196 logger = logging .getLogger ('rio' )
195197 def to_feature (i , geom ):
@@ -200,40 +202,44 @@ def shapes(ctx, src_path, precision, indent, projected):
200202 'geometry' : geom }
201203 try :
202204 collection = {'type' : 'FeatureCollection' }
203- features = []
204- col_xs = []
205- col_ys = []
206205 with rasterio .drivers (CPL_DEBUG = verbosity > 2 ):
207206 with rasterio .open (src_path ) as src :
207+ bounds = src .bounds
208+ xs = [bounds [0 ], bounds [2 ]]
209+ ys = [bounds [1 ], bounds [3 ]]
210+ if not projected :
211+ xs , ys = rasterio .warp .transform (
212+ src .crs , {'init' : 'epsg:4326' }, xs , ys )
213+ if precision >= 0 :
214+ xs = [round (v , precision ) for v in xs ]
215+ ys = [round (v , precision ) for v in ys ]
216+ collection ['bbox' ] = (min (xs ), min (ys ), max (xs ), max (ys ))
208217 if not projected :
209218 transformer = functools .partial (
210219 rasterio .warp .transform ,
211220 src .crs ,
212221 {'init' : 'epsg:4326' })
213- features = [ to_feature (i ,
222+ feature_gen = ( to_feature (i ,
214223 transform_geom (
215224 transformer , g , precision )) for g , i
216225 in rasterio .features .shapes (
217- src .read (1 ), transform = src .affine )]
226+ src .read (1 ), transform = src .affine ))
218227 else :
219- features = [ to_feature (i , g ) for g , i
228+ feature_gen = ( to_feature (i , g ) for g , i
220229 in rasterio .features .shapes (
221- src .read (1 ), transform = src .affine )]
222- bounds = src .bounds
223- xs = [bounds [0 ], bounds [2 ]]
224- ys = [bounds [1 ], bounds [3 ]]
225- if not projected :
226- xs , ys = rasterio .warp .transform (
227- src .crs , {'init' : 'epsg:4326' }, xs , ys )
228- if precision >= 0 :
229- xs = [round (v , precision ) for v in xs ]
230- ys = [round (v , precision ) for v in ys ]
231- col_xs .extend (xs )
232- col_ys .extend (ys )
233- collection ['bbox' ] = [
234- min (col_xs ), min (col_ys ), max (col_xs ), max (col_ys )]
235- collection ['features' ] = features
236- print (json .dumps (collection , indent = indent , sort_keys = True ))
230+ src .read (1 ), transform = src .affine ))
231+ if json_seq :
232+ sys .stdout .write (
233+ json .dumps (collection , indent = indent , sort_keys = True )
234+ + "\n " )
235+ for feature in feature_gen :
236+ sys .stdout .write (
237+ json .dumps (feature , indent = indent , sort_keys = True )
238+ + "\n " )
239+ else :
240+ collection ['features' ] = list (feature_gen )
241+ sys .stdout .write (
242+ json .dumps (collection , indent = indent , sort_keys = True ))
237243 sys .exit (0 )
238244 except Exception :
239245 logger .exception ("Failed. Exception caught" )
0 commit comments