Class PRJDataStore

All Implemented Interfaces:
AutoCloseable, ResourceOnFileSystem, StoreResource, Resource, Localized
Direct Known Subclasses:
RasterStore, WorldFileStore

public abstract class PRJDataStore extends URIDataStore
A data store for a file or URI accompanied by an auxiliary file of the same name with .prj extension. If the auxiliary file is absent, DataOptionKey.DEFAULT_CRS is used as a fallback. The WKT 1 variant used for parsing the "*.prj" file is the variant used by "World Files" and GDAL; this is not the standard specified by OGC 01-009 (they differ in there interpretation of units of measurement).

It is still possible to create a data store with a ReadableByteChannel, InputStream or Reader, in which case the URIDataStore.location will be null and the CRS defined by the DataOptionKey will be used.

Since:
1.2
Version:
1.3
  • Field Details

    • MAXIMAL_LENGTH

      private static final int MAXIMAL_LENGTH
      Maximal length (in bytes) of auxiliary files. This is an arbitrary restriction, we could let the buffer growth indefinitely instead. But a large auxiliary file is probably an error and we do not want an OutOfMemoryError because of that.
      See Also:
    • PRJ

      protected static final String PRJ
      The filename extension of "*.prj" files.
      See Also:
    • encoding

      protected final Charset encoding
      Character encoding in *.prj or other auxiliary files, or null for the JVM default (usually UTF-8).
    • locale

      private final Locale locale
      The locale for texts in *.prj or other auxiliary files, or null for Locale.ROOT (usually English). This locale is not used for parsing numbers or dates.
    • timezone

      private final TimeZone timezone
      Timezone for dates in *.prj or other auxiliary files, or null for UTC.
    • crs

      protected org.opengis.referencing.crs.CoordinateReferenceSystem crs
      The coordinate reference system. This is initialized on the value provided by DataOptionKey.DEFAULT_CRS at construction time, and is modified later if a "*.prj" file is found.
  • Constructor Details

    • PRJDataStore

      protected PRJDataStore(DataStoreProvider provider, StorageConnector connector) throws DataStoreException
      Creates a new data store. This constructor does not open the file, so subclass constructors can decide whether to open in read-only or read/write mode.

      The following options are recognized:

      Parameters:
      provider - the factory that created this PRJDataStore instance, or null if unspecified.
      connector - information about the storage (URL, stream, reader instance, etc).
      Throws:
      DataStoreException - if an error occurred while creating the data store for the given storage.
  • Method Details

    • readPRJ

      protected final void readPRJ() throws DataStoreException
      Reads the "*.prj" auxiliary file. If the file is not found, then this method does nothing and crs keeps its current value (usually the default value found at construction time).

      This method does not verify if it has been invoked multiple time. Caller should track whether the data store has been initialized.

      Throws:
      DataStoreException - if an error occurred while reading the file.
    • readAuxiliaryFile

      protected final PRJDataStore.AuxiliaryContent readAuxiliaryFile(String extension) throws IOException, DataStoreException
      Reads the content of the auxiliary file with the specified extension. This method uses the same URI than URIDataStore.location, except for the extension which is replaced by the given value. This method is suitable for reasonably small files. An arbitrary size limit is applied for safety.
      Parameters:
      extension - the filename extension of the auxiliary file to open.
      Returns:
      the file content together with the source, or null if none. Should be short-lived.
      Throws:
      NoSuchFileException - if the auxiliary file has not been found (when opened from path).
      FileNotFoundException - if the auxiliary file has not been found (when opened from URL).
      IOException - if another error occurred while opening the stream.
      DataStoreException - if the auxiliary file content seems too large.
    • writePRJ

      protected final void writePRJ() throws DataStoreException
      Writes the "*.prj" auxiliary file if crs is non-null. If crs is null and the auxiliary file exists, it is deleted.

      WKT version used

      Current version writes the CRS in WKT 2 format. This is not the common practice, which uses WKT 1. But the WKT 1 variant used by the common practice is not the standard format defined by OGC 01-009. It is more like Convention.WKT1_IGNORE_AXES, which has many ambiguity problems. The WKT 2 format fixes those ambiguities. We hope that major software have updated their referencing engine and can now parse WKT 2 as well as WKT 1.
      Throws:
      DataStoreException - if an error occurred while writing the file.
    • writeAuxiliaryFile

      protected final BufferedWriter writeAuxiliaryFile(String extension) throws IOException, DataStoreException
      Creates a writer for an auxiliary file with the specified extension. This method uses the same path than URIDataStore.location, except for the extension which is replaced by the given value.
      Parameters:
      extension - the filename extension of the auxiliary file to write.
      Returns:
      a stream opened on the specified file.
      Throws:
      UnknownServiceException - if no Path or URI is available.
      DataStoreException - if the auxiliary file cannot be created.
      IOException - if another error occurred while opening the stream.
    • deleteAuxiliaryFile

      protected final void deleteAuxiliaryFile(String extension) throws DataStoreException, IOException
      Deletes the auxiliary file with the given extension if it exists. If the auxiliary file does not exist, then this method does nothing.
      Parameters:
      extension - the filename extension of the auxiliary file to delete.
      Throws:
      DataStoreException - if the auxiliary file is not on a supported file system.
      IOException - if an error occurred while deleting the file.
    • getComponentFiles

      public Path[] getComponentFiles() throws DataStoreException
      Returns the URIDataStore.location as a Path component together with auxiliary files. The default implementation does the same computation as the super-class, then adds the sibling file with ".prj" extension if it exists.
      Specified by:
      getComponentFiles in interface ResourceOnFileSystem
      Overrides:
      getComponentFiles in class URIDataStore
      Returns:
      the main file and auxiliary files as paths, or an empty array if unknown.
      Throws:
      DataStoreException - if the URI cannot be converted to a Path.
    • listComponentFiles

      protected final Path[] listComponentFiles(String... auxiliaries) throws DataStoreException
      Returns the URIDataStore.location as a Path component together with auxiliary files. This method computes the path to the main file as URIDataStore.getComponentFiles(), then add the sibling files with all extensions specified in the auxiliaries argument. Each auxiliary file is tested for existence. Paths that are not regular files are omitted. This is a helper method for getComponentFiles() implementation.
      Parameters:
      auxiliaries - filename extension (without leading dot) of all auxiliary files. Null elements are silently ignored.
      Returns:
      the URI as a path, followed by all auxiliary files that exist.
      Throws:
      DataStoreException - if the URI cannot be converted to a Path.
    • getBaseFilename

      private static String getBaseFilename(Path path)
      Returns the filename of the given path without the file suffix. The returned string always ends in '.', making it ready for concatenation of a new suffix.
    • getOpenParameters

      public Optional<org.opengis.parameter.ParameterValueGroup> getOpenParameters()
      Returns the parameters used to open this data store.
      Overrides:
      getOpenParameters in class URIDataStore
      Returns:
      parameters used for opening this DataStore.
      See Also: