22Description: Validate a STAC item or catalog against the STAC specification.
33
44Usage:
5- stac_validator <stac_file> [--spec_dirs STAC_SPEC_DIRS] [--version STAC_VERSION] [--threads NTHREADS] [--verbose] [--timer] [--log_level LOGLEVEL] [--follow]
5+ stac_validator <stac_file> [--spec_dirs STAC_SPEC_DIRS] [--version STAC_VERSION] [--threads NTHREADS] [--verbose] [--timer] [--log_level LOGLEVEL] [--follow] [--extension EXTENSION]
66
77Arguments:
88 stac_file Fully qualified path or url to a STAC file.
1616 --timer Reports time to validate the STAC. (seconds)
1717 --log_level LOGLEVEL Standard level of logging to report. [default: CRITICAL]
1818 --follow Follow any child links and validate those links. [default: False]
19+ --extension EXTENSION Validate an extension
1920"""
2021
2122import json
3233from urllib .parse import urljoin , urlparse
3334import pystac
3435import pystac .validation
36+ from pystac .serialization import identify_stac_object
3537import requests
3638from docopt import docopt
3739from jsonschema import RefResolutionError , RefResolver , ValidationError , validate
@@ -46,7 +48,7 @@ class VersionException(Exception):
4648
4749
4850class StacValidate :
49- def __init__ (self , stac_file , stac_spec_dirs = None , version = "master" , log_level = "CRITICAL" , follow = False ):
51+ def __init__ (self , stac_file , stac_spec_dirs = None , version = "master" , log_level = "CRITICAL" , follow = False , extension = None ):
5052 """
5153 Validate a STAC file.
5254 :param stac_file: File to validate
@@ -85,6 +87,7 @@ def __init__(self, stac_file, stac_spec_dirs=None, version="master", log_level="
8587 "items" : {"valid" : 0 , "invalid" : 0 },
8688 "unknown" : 0 ,
8789 }
90+ self .extension = extension
8891
8992 @staticmethod
9093 def check_none (input ):
@@ -181,13 +184,27 @@ def fetch_spec(self, spec):
181184
182185 return spec
183186
187+ def get_stac_type (self , stac_content : dict ) -> str :
188+ """Identify the STAC object type
189+ :param stac_content: STAC content dictionary
190+ :type stac_content: dict
191+ :return: STAC object type
192+ :rtype: str
193+ """
194+ stac_object = identify_stac_object (stac_content )
195+ return stac_object .object_type .lower ()
196+
184197 def validate_json (self , stac_content , stac_schema ):
185198 """
186199 Validate STAC.
187200 :param stac_content: input STAC file content
188201 :param stac_schema of STAC (item, catalog, collection)
189202 :return: validation message
190203 """
204+ print (json .dumps (stac_content , indent = 4 ))
205+
206+ #valid - python3 stac_validator.py https://raw.githubusercontent.com/radiantearth/stac-spec/master/item-spec/examples/sample-full.json --extension eo --version 1.0.0-beta.2
207+ #invalid - python3 stac_validator.py https://raw.githubusercontent.com/radiantearth/stac-spec/master/item-spec/examples/sample-full.json --extension eo --version 0.9.0
191208
192209 try :
193210 if "title" in stac_schema and "item" in stac_schema ["title" ].lower ():
@@ -197,8 +214,28 @@ def validate_json(self, stac_content, stac_schema):
197214 "file://" + self .dirpath + "/geojson.json#definitions/feature"
198215 )
199216 logging .info ("Validating STAC" )
217+
200218 pystacVersions = ['0.8.0' ,'0.8.1' ,'0.9.0' ,'1.0.0-beta.2' ]
201- if self .stac_version in pystacVersions :
219+ extension_list = ['checksum' , 'collection-assets' , 'datacube' , 'eo' , 'item-assets' , 'label' , 'pointcloud' ,
220+ 'projection' , 'sar' , 'sat' , 'scientific' , 'single-file-stac' , 'tiled-assets' , 'timestamps' , 'version' , 'view' ]
221+ if (self .extension ):
222+ # if self.extension not in extension_list:
223+ # raise ExtensionException
224+ if self .extension in extension_list :
225+ print (self .extension )
226+
227+ print (self .extension )
228+ # print(self.stac_version)
229+ if (self .extension ):
230+ # message["extension_flag"] = self.extension
231+ stacschema = pystac .validation .JsonSchemaSTACValidator ()
232+ self .stac_type = self .get_stac_type (stac_content )
233+ print (self .stac_type )
234+ print ("here" )
235+ self .stac_type = self .stac_type .upper ()
236+ # self.stac_version = '1.0.0-beta.2'
237+ stacschema .validate_extension (stac_dict = stac_content , stac_object_type = self .stac_type , stac_version = self .stac_version , extension_id = self .extension )
238+ elif self .stac_version in pystacVersions :
202239 pystac .validation .validate_dict (stac_content , stac_version = self .stac_version )
203240 else :
204241 validate (stac_content , stac_schema )
@@ -422,6 +459,7 @@ def main():
422459 stac_file = args .get ("<stac_file>" )
423460 stac_spec_dirs = args .get ("--spec_dirs" , None )
424461 version = args .get ("--version" )
462+ extension = args .get ("--extension" , None )
425463 verbose = args .get ("--verbose" )
426464 nthreads = args .get ("--threads" , 10 )
427465 timer = args .get ("--timer" )
@@ -430,7 +468,7 @@ def main():
430468 if timer :
431469 start = default_timer ()
432470
433- stac = StacValidate (stac_file , stac_spec_dirs , version , log_level , follow )
471+ stac = StacValidate (stac_file , stac_spec_dirs , version , log_level , follow , extension )
434472 _ = stac .run (nthreads )
435473 shutil .rmtree (stac .dirpath )
436474
0 commit comments