/** * Read the envelope based upon the indicator value * * @param envelopeIndicator * @param reader * @return */ private GeometryEnvelope readEnvelope(int envelopeIndicator, ByteReader reader) { GeometryEnvelope envelope = null; if (envelopeIndicator > 0) { // Read x and y values and create envelope double minX = reader.readDouble(); double maxX = reader.readDouble(); double minY = reader.readDouble(); double maxY = reader.readDouble(); boolean hasZ = false; Double minZ = null; Double maxZ = null; boolean hasM = false; Double minM = null; Double maxM = null; // Read z values if (envelopeIndicator == 2 || envelopeIndicator == 4) { hasZ = true; minZ = reader.readDouble(); maxZ = reader.readDouble(); } // Read m values if (envelopeIndicator == 3 || envelopeIndicator == 4) { hasM = true; minM = reader.readDouble(); maxM = reader.readDouble(); } envelope = new GeometryEnvelope(hasZ, hasM); envelope.setMinX(minX); envelope.setMaxX(maxX); envelope.setMinY(minY); envelope.setMaxY(maxY); if (hasZ) { envelope.setMinZ(minZ); envelope.setMaxZ(maxZ); } if (hasM) { envelope.setMinM(minM); envelope.setMaxM(maxM); } } return envelope; }
/** * Populate the geometry data from the bytes * * @param bytes */ public void fromBytes(byte[] bytes) { this.bytes = bytes; ByteReader reader = new ByteReader(bytes); // Get 2 bytes as the magic number and validate String magic = null; try { magic = reader.readString(2); } catch (UnsupportedEncodingException e) { throw new GeoPackageException( "Unexpected GeoPackage Geometry magic number character encoding: Expected: " + GeoPackageConstants.GEO_PACKAGE_GEOMETRY_MAGIC_NUMBER); } if (!magic.equals(GeoPackageConstants.GEO_PACKAGE_GEOMETRY_MAGIC_NUMBER)) { throw new GeoPackageException( "Unexpected GeoPackage Geometry magic number: " + magic + ", Expected: " + GeoPackageConstants.GEO_PACKAGE_GEOMETRY_MAGIC_NUMBER); } // Get a byte as the version and validate, value of 0 = version 1 byte version = reader.readByte(); if (version != GeoPackageConstants.GEO_PACKAGE_GEOMETRY_VERSION_1) { throw new GeoPackageException( "Unexpected GeoPackage Geometry version: " + version + ", Expected: " + GeoPackageConstants.GEO_PACKAGE_GEOMETRY_VERSION_1); } // Get a flags byte and then read the flag values byte flags = reader.readByte(); int envelopeIndicator = readFlags(flags); reader.setByteOrder(byteOrder); // Read the 5th - 8th bytes as the srs id srsId = reader.readInt(); // Read the envelope envelope = readEnvelope(envelopeIndicator, reader); // Save off where the WKB bytes start wkbGeometryIndex = reader.getNextByte(); // Read the Well-Known Binary Geometry if not marked as empty if (!empty) { geometry = WkbGeometryReader.readGeometry(reader); } }