Pynitf Reference¶
NitfFile¶
- class pynitf.NitfFile(file_name=None, security=<pynitf.nitf_security.NitfSecurity object>)¶
This is used to read and write a NITF File.
- Variables
file_header – The NitfFileHeader for the file
file_name – The NITF file name
segment_hook_set – The NitfSegmentHookSet to use for the file.
user_subheader_handle_set – The NitfSegmentUserSubheaderHandleSet to use for this file
data_handle_set – The NitfSegmentDataHandleSet to use for this file
report_raw – If True, suppress NitfSegmentHook when printing
image_segment – List of NitfImageSegment objects for the file.
graphic_segment – List of NitfGraphicSegment objects for the file.
text_segment – List of NitfTextSegment objects for the file.
des_segment – List of NitfDesSegment objects for the file.
res_segment – List of NitfResSegment objects for the file.
tre_list – List of Tre objects for the file level TREs.
- property engrda¶
Return access to all the ENGRDA TREs as a hash on resrc (which should be unique). Error occurs if resrc is not unique.
- find_exactly_one_tre(tre_tag)¶
Like find_one_tre, but not finding the TRE is treated as an error
- find_one_tre(tre_tag)¶
Find the given TRE. If not found, return None. If found, return it, if more than one found, return an error
- iseg_by_idlvl(idlvl)¶
Return the image segment with a idlvl matching the given id
- iseg_by_iid1(iid1)¶
Return a (possibly empty) list of image segments with the given iid1 value.
- iseg_by_iid1_single(iid1)¶
Return a single match to the given iid1. If we have 0 or more than 1 match, then this throws an error.
- read(file_name)¶
Read the given file.
- property security¶
NitfSecurity for file.
- segments(include_seg_index=False)¶
Iterator to go through all the segments in a file. We often also need the seg_index, so you can pass that as True and we return the pair (seg_index, seg).
Note that although this goes through all the segment types, the seg_index is relative to the segment type. So for example, the first graphic segment returns (0, gseg_0) even though this might be the 10th segment in the file. This is just the way that NITF wants these listed.
Just to avoid confusion, the seg_index are always 0 based. NITF Files tend to use 1 based indexing, we handle the conversion to and from 1 based indexing at the lowest level of the file access.
- summary()¶
Short text summary of this file, something you can print out
- write(file_name)¶
Write to the given file
NitfSegmentHookSet¶
- class pynitf.NitfSegmentHookSet(iterable=())¶
- To allow special handling of TREs etc. we allow a hook_list of
these objects to be passed to NitfSegment. These then are called in each function of NitfSegment.
See for example geocal_nitf_rsm.py in geocal for an example of using these hooks to add in support for the geocal Rsm object.
This class handles the full set of NitfSegmentHook that are applied. NitfFile contains a NitfSegmentHookSet as the variable segment_hook_set. The initial default value of this is given by default_hook_set, an individual NitfFile object might modify this for some reason (e.g., add a special hook, remove a hook).
- Variables
hook_set – The set of NitfSegmentHook to apply to NitfSegments in a NitfFile
- classmethod add_default_hook(h)¶
Add the given hook to the default set of hookrs. The higher priority_order (larger number) items are tried first.
- after_append_hook(seg, nitf_file)¶
Called after a segment is added to a NitfFile
- after_init_hook(seg, nitf_file)¶
Called at the end of NitfSegment.__init__
- after_read_hook(seg, nitf_file)¶
Called after NitfFile has read a file
- before_str_hook(seg, nitf_file, fh)¶
Called at the start of NitfSegment.__str__
- before_str_tre_hook(seg, tre, nitf_file, fh)¶
Called before printing a TRE. If this returns true we assume that this class has handled the TRE printing. Otherwise, we call print on the tre
- before_write_hook(seg, nitf_file)¶
Called before NitfFile writes a file
- classmethod default_hook_set()¶
Return the default set of hookrs to use.
- classmethod discard_default_hook(h)¶
Discard the hook h from the default list. It is ok if h isn’t actually in the set of hooks.
NitfSegmentHook¶
- class pynitf.NitfSegmentHook¶
To allow special handling of TREs etc. we allow a hook_list of these objects to be passed to NitfSegment. These then are called in each function of NitfSegment.
Note that you don’t need to strictly derive from this class, we use the standard “duck” typing of python. This is just the list of functions that need to be supplied.
See for example geocal_nitf_rsm.py in geocal for an example of using these hooks to add in support for the geocal Rsm object.
- after_append_hook(seg, nitf_file)¶
Called when a segment is added to a NitfFile
- after_init_hook(seg, nitf_file)¶
Called at the end of NitfSegment.__init__
- after_read_hook(seg, nitf_file)¶
Called after NitfFile has read a file
- before_str_hook(seg, nitf_file, fh)¶
Called at the start of NitfSegment.__str__
- before_str_tre_hook(seg, tre, nitf_file, fh)¶
Called before printing a TRE. If this returns true we assume that this class has handled the TRE printing. Otherwise, we call print on the tre
- before_write_hook(seg, nitf_file)¶
Called before NitfFile writes a file
- remove_for_report_raw()¶
Hooks usually map to some higher level object (e.g., Geocal handling RSM. Normally you want this, but for certain contexts it can be useful to suppress this behavior, e.g., nitfinfofull reporting the raw TRE data rather than the objects generated by the TRE data.
This skips before_str_hook and before_str_tre_hook if we are reporting raw information if this function returns True. The hooks are by default marked as “True” for removing, but if you have some special case where you want to avoid removing the hook you can have the derived class change this to False.
PriorityHandleSet¶
- class pynitf.PriorityHandleSet¶
- classmethod add_default_handle(h, priority_order=0)¶
Add the given handle to the default set of handlers. The higher priority_order (larger number) items are tried first.
- add_handle(h, priority_order=0)¶
Add a handler. The higher priority_order (larger number) items are tried first.
- clear()¶
Remove all handles in the set.
- classmethod default_handle_set()¶
Return the default set of handlers to use.
- classmethod discard_default_handle(h)¶
Discard the handle h from the default list. It is ok if h isn’t actually in the set of handles.
- discard_handle(h)¶
Discard the handle h. It is ok if h isn’t actually in the set of handles.
- handle(*args, **keywords)¶
Find the first handle that says it can process the given arguments, and return the results from that handle.
NitfSegmentUserSubheaderHandleSet¶
UserSubheaderHandle¶
- class pynitf.UserSubheaderHandle¶
DesIdToUSHHandle¶
ResIdToUSHHandle¶
NitfSegmentDataHandleSet¶
- class pynitf.NitfSegmentDataHandleSet¶
Handle reading the data in a segment (e.g, a image)
- handle_h(cls, seg, fh, seg_index)¶
Try reading using a given cls derived from NitfData. We first check that the seg is the same class as cls.seg_class, and if so try to read the data from file.
- read_from_file(seg, fh, seg_index=None)¶
Read the data for the given NitfSegment from file handle fh.
Note that most handlers don’t care about the seg_index, but there are some that hand things off other libraries that do use this information (e.g. NitfImageGdal found in GeoCal). So we hand off this information if we have the seg_index available (e.g., we are calling this through NitfFile).
NitfData¶
- class pynitf.NitfData(seg=None)¶
Handle reading and writing the data in a segment (e.g, a image).
- abstract read_from_file(fh, seg_index=None)¶
Attempt to read data from the given file. If the data can’t be read this class, return False. Otherwise, return True. Note that True/False isn’t from a read error, but rather because this is an unsupported type (e.g., a JPEG-2000 compressed image with a reader that doesn’t support that).
- property security¶
NitfSecurity for data.
- property subheader¶
Return subheader for NitfData
- property user_subheader¶
Return user_subheader for NitfData
- property user_subheader_size¶
Return the size of the user subheader. This can be used to make sure we aren’t exceeding the size supported by desshl
- abstract write_to_file(fh)¶
Write data to the given file handle.
NitfImage¶
NitfDes¶
NitfText¶
NitfGraphic¶
NitfDataPlaceHolder¶
- class pynitf.NitfDataPlaceHolder(seg=None)¶
Implementation that doesn’t actually read any data, useful as a final place holder if none of our other NitfData classes can handle a particular segment. We just skip over the data when reading.
- read_from_file(fh, seg_index=None)¶
Attempt to read data from the given file. If the data can’t be read this class, return False. Otherwise, return True. Note that True/False isn’t from a read error, but rather because this is an unsupported type (e.g., a JPEG-2000 compressed image with a reader that doesn’t support that).
- write_to_file(fh)¶
Write data to the given file handle.