Java Code Examples for org.locationtech.jts.geom.Envelope#getMaxY()

The following examples show how to use org.locationtech.jts.geom.Envelope#getMaxY() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LasIndexer.java    From hortonmachine with GNU General Public License v3.0 7 votes vote down vote up
public static Polygon envelopeToPolygon( Envelope envelope ) {
    double w = envelope.getMinX();
    double e = envelope.getMaxX();
    double s = envelope.getMinY();
    double n = envelope.getMaxY();

    Coordinate[] coords = new Coordinate[5];
    coords[0] = new Coordinate(w, n);
    coords[1] = new Coordinate(e, n);
    coords[2] = new Coordinate(e, s);
    coords[3] = new Coordinate(w, s);
    coords[4] = new Coordinate(w, n);

    GeometryFactory gf = GeometryUtilities.gf();
    LinearRing linearRing = gf.createLinearRing(coords);
    Polygon polygon = gf.createPolygon(linearRing, null);
    return polygon;
}
 
Example 2
Source File: ASpatialDb.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Reproject an envelope.
 * 
 * @param fromEnvelope the original envelope.
 * @param fromSrid the original srid.
 * @param toSrid the destination srid.
 * @return the reprojected Envelope.
 * @throws Exception
 */
public Envelope reproject( Envelope fromEnvelope, int fromSrid, int toSrid ) throws Exception {
    double w = fromEnvelope.getMinX();
    double e = fromEnvelope.getMaxX();
    double s = fromEnvelope.getMinY();
    double n = fromEnvelope.getMaxY();
    String sql = "select ST_Transform( ST_PointFromText('POINT( " + w + " " + s + ")', " + fromSrid + ") , " + toSrid
            + "), ST_Transform( ST_PointFromText('POINT( " + e + " " + n + ")', " + fromSrid + ") , " + toSrid + ")";
    return execOnConnection(connection -> {
        IGeometryParser gp = getType().getGeometryParser();
        try (IHMStatement stmt = connection.createStatement(); IHMResultSet rs = stmt.executeQuery(sql)) {
            if (rs.next()) {
                Geometry llPoint = gp.fromResultSet(rs, 1);
                Geometry urPoint = gp.fromResultSet(rs, 2);
                if (llPoint instanceof Point) {
                    Point ll = (Point) llPoint;
                    Point ur = (Point) urPoint;
                    Envelope newEnv = new Envelope(ll.getX(), ur.getX(), ll.getY(), ur.getY());
                    return newEnv;
                }
            }
            return null;
        }
    });
}
 
Example 3
Source File: DatabaseLasDataManager.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
@Override
public synchronized List<Geometry> getEnvelopesInGeometry( Geometry checkGeom, boolean doOnlyEnvelope, double[] minMaxZ )
        throws Exception {
    checkOpen();

    List<LasCell> lasCells = LasCellsTable.getLasCells(spatialDb, checkGeom, true, false, false, false, false);

    double minZ = Double.POSITIVE_INFINITY;
    double maxZ = Double.NEGATIVE_INFINITY;
    Envelope env = new Envelope();
    for( LasCell lasCell : lasCells ) {
        minZ = Math.min(minZ, lasCell.minElev);
        maxZ = Math.max(maxZ, lasCell.maxElev);
        env.expandToInclude(lasCell.polygon.getEnvelopeInternal());
    }

    ReferencedEnvelope3D dataEnvelope = new ReferencedEnvelope3D(env.getMinX(), env.getMaxX(), env.getMinY(), env.getMaxY(),
            minZ, maxZ, crs);
    Polygon envelopePolygon = LasIndexer.envelopeToPolygon(dataEnvelope);
    ArrayList<Geometry> envelopeList = new ArrayList<Geometry>();
    envelopeList.add(envelopePolygon);
    return envelopeList;
}
 
Example 4
Source File: SpatialiteCommonMethods.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
public static String getSpatialindexGeometryWherePiece( ASpatialDb db, String tableName, String alias, Geometry geometry )
        throws Exception {
    String rowid = "";
    if (alias == null) {
        alias = "";
        rowid = tableName + ".ROWID";
    } else {
        rowid = alias + ".ROWID";
        alias = alias + ".";
    }

    Envelope envelope = geometry.getEnvelopeInternal();
    double x1 = envelope.getMinX();
    double x2 = envelope.getMaxX();
    double y1 = envelope.getMinY();
    double y2 = envelope.getMaxY();
    GeometryColumn gCol = db.getGeometryColumnsForTable(tableName);
    if (tableName.indexOf('.') != -1) {
        // if the tablename contains a dot, then it comes from an attached
        // database
        tableName = "DB=" + tableName;
    }
    String sql = "ST_Intersects(" + alias + gCol.geometryColumnName + ", " + "ST_GeomFromText('" + geometry.toText() + "')"
            + ") = 1 AND " + rowid + " IN ( SELECT ROWID FROM SpatialIndex WHERE "//
            + "f_table_name = '" + tableName + "' AND " //
            + "search_frame = BuildMbr(" + x1 + ", " + y1 + ", " + x2 + ", " + y2 + "))";
    return sql;
}
 
Example 5
Source File: GridH2SpatialIndex.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param row Row.
 * @param rowId Row id.
 * @return Envelope.
 */
private SpatialKey getEnvelope(SearchRow row, long rowId) {
    Value v = row.getValue(columnIds[0]);
    Geometry g = ((ValueGeometry) v.convertTo(Value.GEOMETRY)).getGeometry();
    Envelope env = g.getEnvelopeInternal();
    return new SpatialKey(rowId,
        (float) env.getMinX(), (float) env.getMaxX(),
        (float) env.getMinY(), (float) env.getMaxY());
}
 
Example 6
Source File: MercatorUtils.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns bounds of the given tile in EPSG:3857 coordinates
 *
 * @param tx       tile x.
 * @param ty       tile y.
 * @param zoom     zoomlevel.
 * @return the Envelope.
 */
public static Envelope tileBounds3857( final int x, final int y, final int zoom ) {
    Envelope env4326 = tileBounds4326(x, y, zoom);
    Coordinate ll4326 = new Coordinate(env4326.getMinX(), env4326.getMinY());
    Coordinate ur4326 = new Coordinate(env4326.getMaxX(), env4326.getMaxY());

    Coordinate ll3857transf = MercatorUtils.convert4326To3857(ll4326);
    Coordinate ur3857transf = MercatorUtils.convert4326To3857(ur4326);

    return new Envelope(ll3857transf, ur3857transf);
}
 
Example 7
Source File: MercatorUtils.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
public static Envelope convert4326To3857( Envelope envelope4326 ) {
    Coordinate ll4326 = new Coordinate(envelope4326.getMinX(), envelope4326.getMinY());
    Coordinate ur4326 = new Coordinate(envelope4326.getMaxX(), envelope4326.getMaxY());

    Coordinate ll3857 = convert4326To3857(ll4326);
    Coordinate ur3857 = convert4326To3857(ur4326);

    Envelope env3857 = new Envelope(ll3857, ur3857);
    return env3857;
}
 
Example 8
Source File: MercatorUtils.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
public static Envelope convert3857To4326( Envelope envelope3857 ) {
    Coordinate ll3857 = new Coordinate(envelope3857.getMinX(), envelope3857.getMinY());
    Coordinate ur3857 = new Coordinate(envelope3857.getMaxX(), envelope3857.getMaxY());

    Coordinate ll4326 = convert3857To4326(ll3857);
    Coordinate ur4326 = convert3857To4326(ur3857);

    Envelope env4326 = new Envelope(ll4326, ur4326);
    return env4326;
}
 
Example 9
Source File: OmsRasterReader.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
private void readGrass( File mapFile ) throws Exception {
    JGrassMapEnvironment mapEnvironment = new JGrassMapEnvironment(new File(file));
    CoordinateReferenceSystem crs = mapEnvironment.getCoordinateReferenceSystem();
    JGrassRegion readRegion = mapEnvironment.getFileRegion();
    double n = readRegion.getNorth();
    double s = readRegion.getSouth();
    double w = readRegion.getWest();
    double e = readRegion.getEast();

    Envelope env = readRegion.getEnvelope();
    originalEnvelope = new GeneralEnvelope(
            new ReferencedEnvelope(env.getMinX(), env.getMaxX(), env.getMinY(), env.getMaxY(), crs));

    // if bounds supplied, use them as region
    if (pBounds != null) {
        // n, s, w, e
        n = pBounds[0];
        s = pBounds[1];
        w = pBounds[2];
        e = pBounds[3];
    }
    if (pRes != null) {
        readRegion = new JGrassRegion(w, e, s, n, pRes[0], pRes[1]);
    }
    if (pRowcol != null) {
        readRegion = new JGrassRegion(w, e, s, n, pRowcol[0], pRowcol[1]);
    }

    if (!doEnvelope) {
        if (generalParameter == null) {
            generalParameter = createGridGeometryGeneralParameter(readRegion.getCols(), readRegion.getRows(),
                    readRegion.getNorth(), readRegion.getSouth(), readRegion.getEast(), readRegion.getWest(), crs);
        }
        GrassCoverageFormat format = new GrassCoverageFormatFactory().createFormat();
        GrassCoverageReader reader = format.getReader(mapEnvironment.getCELL());
        outRaster = (GridCoverage2D) reader.read(generalParameter);
        checkNovalues();
    }
}
 
Example 10
Source File: TestSpatialDbsMain.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testReprojectFromDb() throws Exception {

    double w = 11.143413001499738;
    double e = 11.147502220729288;
    double s = 46.62897848892326;
    double n = 46.62981208577648;

    Envelope env = new Envelope(w, e, s, n);
    
    Envelope reprojected = db.reproject(env, 4326, 32632);
    double rw = reprojected.getMinX();
    double re = reprojected.getMaxX();
    double rs = reprojected.getMinY();
    double rn = reprojected.getMaxY();
    
    assertEquals(664076.6777860201, rw, 0.001);
    assertEquals(664387.1714802807, re, 0.001);
    assertEquals(5166166.626361137, rs, 0.001);
    assertEquals(5166267.775614383, rn, 0.001);
    
    
    
    WKTReader reader = new WKTReader();
    Geometry point = reader.read("POINT (11.143413001499738 46.62897848892326)");
    Geometry reprojectedGeom = db.reproject(point, 4326, 32632);
    Geometry expected = reader.read("POINT (664076.6777860201 5166166.626361137)");
    
    double distance = reprojectedGeom.distance(expected);
    assertEquals(0.0, distance, 0.00001);
    
}
 
Example 11
Source File: GeometryUtils.java    From geowave with Apache License 2.0 5 votes vote down vote up
/**
 * Generate a latitude range from a JTS geometry
 *
 * @param geometry The JTS geometry
 * @return The y range
 */
public static NumericData yRangeFromGeometry(final Geometry geometry) {
  if ((geometry == null) || geometry.isEmpty()) {
    return new NumericRange(0, 0);
  }
  // Get the envelope of the geometry being held
  final Envelope env = geometry.getEnvelopeInternal();

  // Create a NumericRange object using the y axis
  return new NumericRange(env.getMinY(), env.getMaxY());
}
 
Example 12
Source File: GeopackageTableLayer.java    From geopaparazzi with GNU General Public License v3.0 5 votes vote down vote up
private void updateGeometry(ASpatialDb db, String tableName, long id, Geometry geometry) throws Exception {
    int epsg = 4326;
    String pk = ((GPGeopackageDb) db).getPrimaryKey(tableName);

    IGeometryParser gp = db.getType().getGeometryParser();
    geometry.setSRID(epsg);
    Object obj = gp.toSqlObject(geometry);
    if (obj instanceof byte[]) {
        byte[] objBytes = (byte[]) obj;
        GeometryColumn gc = db.getGeometryColumnsForTable(tableName);
        String sql = "update " + tableName + " set " + gc.geometryColumnName + "=? where " + pk + "=" + id;

        db.execOnConnection(connection -> {
            try (IHMPreparedStatement pStmt = connection.prepareStatement(sql)) {
                pStmt.setBytes(1, objBytes);
                pStmt.executeUpdate();
            }
            return null;
        });

        Envelope env = geometry.getEnvelopeInternal();
        double minX = env.getMinX();
        double maxX = env.getMaxX();
        double minY = env.getMinY();
        double maxY = env.getMaxY();

        try {
            // also update rtree index, since it is not supported
            String sqlTree = "INSERT OR REPLACE INTO rtree_" + tableName + "_" + gc.geometryColumnName +
                    " VALUES (" + id + "," + minX + ", " + maxX + "," + minY + ", " + maxY + ");";
            db.executeInsertUpdateDeleteSql(sqlTree);
        } catch (Exception e) {
            GPLog.error(this, "ERROR on rtree", e);
        }

    } else {
        throw new IllegalArgumentException("Geometry object is not byte array.");
    }
}
 
Example 13
Source File: TransformationUtils.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Given a transformation, transform an envelope by it.
 * 
 * @param transformation the AffineTransformation to use.
 * @param env the envelope to transform.
 * @return the transformed envelope.
 */
public static Envelope transformEnvelope( AffineTransformation transformation, Envelope env ) {
    Coordinate llFromPoint = new Coordinate(env.getMinX(), env.getMinY());
    Coordinate urFromPoint = new Coordinate(env.getMaxX(), env.getMaxY());
    Coordinate ll = new Coordinate();
    Coordinate ur = new Coordinate();
    transformation.transform(llFromPoint, ll);
    transformation.transform(urFromPoint, ur);
    return new Envelope(ll.getX(), ur.getX(), ll.getY(), ur.getY());
}
 
Example 14
Source File: GeometryWrapper.java    From geowave with Apache License 2.0 5 votes vote down vote up
/** Expects Longitude before Latitude */
@Override
public boolean overlaps(final NumericDimensionField[] fields, final NumericData[] rangeData) {

  final int latPosition = fields[0] instanceof LatitudeField ? 0 : 1;
  final int longPosition = fields[0] instanceof LatitudeField ? 1 : 0;
  if (fields.length == 1) {
    final Envelope env = geometry.getEnvelopeInternal();
    final NumericRange r =
        latPosition == 0 ? new NumericRange(env.getMinY(), env.getMaxY())
            : new NumericRange(env.getMinX(), env.getMaxX());
    return ((rangeData[0].getMin() < r.getMax())
        || DoubleMath.fuzzyEquals(rangeData[0].getMin(), r.getMax(), DOUBLE_TOLERANCE))
        && ((rangeData[0].getMax() > r.getMin())
            || DoubleMath.fuzzyEquals(rangeData[0].getMax(), r.getMin(), DOUBLE_TOLERANCE));
  }
  return geometry.getFactory().createPolygon(
      new Coordinate[] {
          new Coordinate(
              rangeData[longPosition].getMin() - DOUBLE_TOLERANCE,
              rangeData[latPosition].getMin() - DOUBLE_TOLERANCE),
          new Coordinate(
              rangeData[longPosition].getMin() - DOUBLE_TOLERANCE,
              rangeData[latPosition].getMax() + DOUBLE_TOLERANCE),
          new Coordinate(
              rangeData[longPosition].getMax() + DOUBLE_TOLERANCE,
              rangeData[latPosition].getMax() + DOUBLE_TOLERANCE),
          new Coordinate(
              rangeData[longPosition].getMax() + DOUBLE_TOLERANCE,
              rangeData[latPosition].getMin() - DOUBLE_TOLERANCE),
          new Coordinate(
              rangeData[longPosition].getMin() - DOUBLE_TOLERANCE,
              rangeData[latPosition].getMin() - DOUBLE_TOLERANCE)}).intersects(geometry);
}
 
Example 15
Source File: LasCellsTable.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Query the las cell table.
 *
 * @param db the db to use.
 * @param envelope an optional {@link Envelope} to query spatially.
 * @param exactGeometry an optional exact geometry. If available it is used instead of the envelope.
 * @param doPosition if <code>true</code> position info is extracted.
 * @param doIntensity if <code>true</code> intensity and classification info is extracted.
 * @param doReturns  if <code>true</code> return info is extracted.
 * @param doTime  if <code>true</code> time info is extracted.
 * @param doColor if <code>true</code> color info is extracted.
 * @param limitTo limit the cells to a value if != -1
 * @return the list of extracted points
 * @throws Exception
 */
public static List<LasCell> getLasCells( ASpatialDb db, Envelope envelope, Geometry exactGeometry, boolean doPosition,
        boolean doIntensity, boolean doReturns, boolean doTime, boolean doColor, int limitTo ) throws Exception {
    List<LasCell> lasCells = new ArrayList<>();
    String sql = "SELECT " + COLUMN_GEOM + "," + COLUMN_ID + "," + COLUMN_SOURCE_ID + "," + COLUMN_POINTS_COUNT;

    if (doPosition)
        sql += "," + COLUMN_AVG_ELEV + "," + //
                COLUMN_MIN_ELEV + "," + //
                COLUMN_MAX_ELEV + "," + //
                COLUMN_POSITION_BLOB;//

    if (doIntensity)
        sql += "," + COLUMN_AVG_INTENSITY + "," + //
                COLUMN_MIN_INTENSITY + "," + //
                COLUMN_MAX_INTENSITY + "," + //
                COLUMN_INTENS_CLASS_BLOB;//

    if (doReturns)
        sql += "," + COLUMN_RETURNS_BLOB;

    if (doTime)
        sql += "," + COLUMN_MIN_GPSTIME + "," + //
                COLUMN_MAX_GPSTIME + "," + //
                COLUMN_GPSTIME_BLOB;
    if (doColor)
        sql += "," + COLUMN_COLORS_BLOB;

    sql += " FROM " + TABLENAME;

    if (exactGeometry != null) {
        sql += " WHERE " + db.getSpatialindexGeometryWherePiece(TABLENAME, null, exactGeometry);
    } else if (envelope != null) {
        double x1 = envelope.getMinX();
        double y1 = envelope.getMinY();
        double x2 = envelope.getMaxX();
        double y2 = envelope.getMaxY();
        sql += " WHERE " + db.getSpatialindexBBoxWherePiece(TABLENAME, null, x1, y1, x2, y2);
    }

    if (limitTo > 0) {
        sql += " LIMIT " + limitTo;
    }

    String _sql = sql;
    IGeometryParser gp = db.getType().getGeometryParser();
    return db.execOnConnection(conn -> {
        try (IHMStatement stmt = conn.createStatement(); IHMResultSet rs = stmt.executeQuery(_sql)) {
            while( rs.next() ) {
                LasCell lasCell = resultSetToCell(db, gp, doPosition, doIntensity, doReturns, doTime, doColor, rs);
                lasCells.add(lasCell);
            }
            return lasCells;
        }
    });

}
 
Example 16
Source File: LasLevelsTable.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Query the las level table.
 *
 * @param db the db to use.
 * @param levelNum the level to query.
 * @param envelope an optional {@link Envelope} to query spatially.
 * @return the list of extracted level cells.
 * @throws Exception
 */
public static List<LasLevel> getLasLevels( ASpatialDb db, int levelNum, Envelope envelope ) throws Exception {
    String tableName = TABLENAME + levelNum;
    List<LasLevel> lasLevels = new ArrayList<>();
    String sql = "SELECT " + COLUMN_GEOM + "," + //
            COLUMN_ID + "," + COLUMN_SOURCE_ID + "," + COLUMN_AVG_ELEV + "," + //
            COLUMN_MIN_ELEV + "," + //
            COLUMN_MAX_ELEV + "," + //
            COLUMN_AVG_INTENSITY + "," + //
            COLUMN_MIN_INTENSITY + "," + //
            COLUMN_MAX_INTENSITY;

    sql += " FROM " + tableName;

    if (envelope != null) {
        double x1 = envelope.getMinX();
        double y1 = envelope.getMinY();
        double x2 = envelope.getMaxX();
        double y2 = envelope.getMaxY();
        sql += " WHERE " + db.getSpatialindexBBoxWherePiece(tableName, null, x1, y1, x2, y2);
    }

    String _sql = sql;
    IGeometryParser gp = db.getType().getGeometryParser();
    return db.execOnConnection(conn -> {
        try (IHMStatement stmt = conn.createStatement(); IHMResultSet rs = stmt.executeQuery(_sql)) {
            while( rs.next() ) {
                LasLevel lasLevel = new LasLevel();
                lasLevel.level = levelNum;
                int i = 1;
                Geometry geometry = gp.fromResultSet(rs, i++);
                if (geometry instanceof Polygon) {
                    Polygon polygon = (Polygon) geometry;
                    lasLevel.polygon = polygon;
                    lasLevel.id = rs.getLong(i++);
                    lasLevel.sourceId = rs.getLong(i++);
                    lasLevel.avgElev = rs.getDouble(i++);
                    lasLevel.minElev = rs.getDouble(i++);
                    lasLevel.maxElev = rs.getDouble(i++);
                    lasLevel.avgIntensity = rs.getShort(i++);
                    lasLevel.minIntensity = rs.getShort(i++);
                    lasLevel.maxIntensity = rs.getShort(i++);
                    lasLevels.add(lasLevel);
                }
            }
            return lasLevels;
        }
    });

}
 
Example 17
Source File: ASpatialDb.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Get the geometries of a table inside a given envelope.
 * 
 * @param tableName
 *            the table name.
 * @param envelope
 *            the envelope to check.
 * @param prePostWhere an optional set of 3 parameters. The parameters are: a 
 *          prefix wrapper for geom, a postfix for the same and a where string 
 *          to apply. They all need to be existing if the parameter is passed.
 * @return The list of geometries intersecting the envelope.
 * @throws Exception
 */
public List<Geometry> getGeometriesIn( String tableName, Envelope envelope, String... prePostWhere ) throws Exception {
    List<String> wheres = new ArrayList<>();
    String pre = "";
    String post = "";
    String where = "";
    if (prePostWhere != null && prePostWhere.length == 3) {
        if (prePostWhere[0] != null)
            pre = prePostWhere[0];
        if (prePostWhere[1] != null)
            post = prePostWhere[1];
        if (prePostWhere[2] != null) {
            where = prePostWhere[2];
            wheres.add(where);
        }
    }

    GeometryColumn gCol = getGeometryColumnsForTable(tableName);
    String sql = "SELECT " + pre + gCol.geometryColumnName + post + " FROM " + DbsUtilities.fixTableName(tableName);

    if (envelope != null && supportsSpatialIndex) {
        double x1 = envelope.getMinX();
        double y1 = envelope.getMinY();
        double x2 = envelope.getMaxX();
        double y2 = envelope.getMaxY();
        String spatialindexBBoxWherePiece = getSpatialindexBBoxWherePiece(tableName, null, x1, y1, x2, y2);
        if (spatialindexBBoxWherePiece != null)
            wheres.add(spatialindexBBoxWherePiece);
    }

    if (wheres.size() > 0) {
        sql += " WHERE " + DbsUtilities.joinBySeparator(wheres, " AND ");
    }

    String _sql = sql;
    IGeometryParser geometryParser = getType().getGeometryParser();
    return execOnConnection(connection -> {
        List<Geometry> geoms = new ArrayList<Geometry>();
        try (IHMStatement stmt = connection.createStatement(); IHMResultSet rs = stmt.executeQuery(_sql)) {
            while( rs.next() ) {
                Geometry geometry = geometryParser.fromResultSet(rs, 1);
                if (!supportsSpatialIndex && envelope != null) {
                    // need to check manually
                    if (!geometry.getEnvelopeInternal().intersects(envelope)) {
                        continue;
                    }
                }
                geoms.add(geometry);
            }
        }
        return geoms;
    });
}
 
Example 18
Source File: GeometryUtils.java    From geowave with Apache License 2.0 4 votes vote down vote up
public static MultiDimensionalNumericData getBoundsFromEnvelope(final Envelope envelope) {
  final NumericRange[] boundsPerDimension = new NumericRange[2];
  boundsPerDimension[0] = new NumericRange(envelope.getMinX(), envelope.getMaxX());
  boundsPerDimension[1] = new NumericRange(envelope.getMinY(), envelope.getMaxY());
  return new BasicNumericDataset(boundsPerDimension);
}
 
Example 19
Source File: GeopackageTableLayer.java    From geopaparazzi with GNU General Public License v3.0 4 votes vote down vote up
private long insertGeometry(ASpatialDb db, String tableName, Geometry geometry) throws Exception {
    int epsg = 4326;
    String pk = ((GPGeopackageDb) db).getPrimaryKey(tableName);
    long nextId = db.getLong("select max(" + pk + ") from " + tableName) + 1;

    IGeometryParser gp = db.getType().getGeometryParser();
    geometry.setSRID(epsg);
    Object obj = gp.toSqlObject(geometry);
    if (obj instanceof byte[]) {
        byte[] objBytes = (byte[]) obj;
        GeometryColumn gc = db.getGeometryColumnsForTable(tableName);
        String sql = "INSERT INTO " + tableName + " (" + pk + "," + gc.geometryColumnName + ") VALUES (?, ?)";

        db.execOnConnection(connection -> {
            try (IHMPreparedStatement pStmt = connection.prepareStatement(sql)) {
                pStmt.setLong(1, nextId);
                pStmt.setBytes(2, objBytes);
                pStmt.executeUpdate();
            }
            return null;
        });

        Envelope env = geometry.getEnvelopeInternal();
        double minX = env.getMinX();
        double maxX = env.getMaxX();
        double minY = env.getMinY();
        double maxY = env.getMaxY();

        try {
            // also update rtree index, since it is not supported
            String sqlTree = "INSERT OR REPLACE INTO rtree_" + tableName + "_" + gc.geometryColumnName +
                    " VALUES (" + nextId + "," + minX + ", " + maxX + "," + minY + ", " + maxY + ");";
            db.executeInsertUpdateDeleteSql(sqlTree);
        } catch (Exception e) {
            GPLog.error(this, "ERROR on rtree", e);
        }

        return nextId;
    }

    throw new IllegalArgumentException("Geometry object is not byte array.");
}
 
Example 20
Source File: RegionMap.java    From hortonmachine with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Creates a new {@link RegionMap} cropped on the new bounds and snapped on the original grid.
 * 
 * <p><b>The supplied bounds are contained in the resulting region.</b></p>
 * 
 * @param envelope the envelope to snap.
 * @return the new {@link RegionMap}.
 */
public RegionMap toSubRegion( Envelope envelope ) {
    double w = envelope.getMinX();
    double s = envelope.getMinY();
    double e = envelope.getMaxX();
    double n = envelope.getMaxY();
    return toSubRegion(n, s, w, e);
}