private GeometryDescriptor reprojectGeometry(GeometryDescriptor descr) {
   if (descr == null) {
     return null;
   }
   GeometryType type =
       ftf.createGeometryType(
           descr.getType().getName(),
           descr.getType().getBinding(),
           reprojection,
           descr.getType().isIdentified(),
           descr.getType().isAbstract(),
           descr.getType().getRestrictions(),
           descr.getType().getSuper(),
           descr.getType().getDescription());
   type.getUserData().putAll(descr.getType().getUserData());
   GeometryDescriptor gd =
       ftf.createGeometryDescriptor(
           type,
           descr.getName(),
           descr.getMinOccurs(),
           descr.getMaxOccurs(),
           descr.isNillable(),
           descr.getDefaultValue());
   gd.getUserData().putAll(descr.getUserData());
   return gd;
 }
Exemplo n.º 2
0
  @Override
  public void encodeGeometryColumn(
      GeometryDescriptor gatt, String prefix, int srid, Hints hints, StringBuffer sql) {

    boolean geography =
        "geography".equals(gatt.getUserData().get(JDBCDataStore.JDBC_NATIVE_TYPENAME));

    if (geography) {
      sql.append("encode(ST_AsBinary(");
      encodeColumnName(prefix, gatt.getLocalName(), sql);
      sql.append("),'base64')");
    } else {
      boolean force2D =
          hints != null
              && hints.containsKey(Hints.FEATURE_2D)
              && Boolean.TRUE.equals(hints.get(Hints.FEATURE_2D));

      if (force2D) {
        sql.append("encode(ST_AsBinary(ST_Force_2D(");
        encodeColumnName(prefix, gatt.getLocalName(), sql);
        sql.append(")),'base64')");
      } else {
        sql.append("encode(ST_AsEWKB(");
        encodeColumnName(prefix, gatt.getLocalName(), sql);
        sql.append("),'base64')");
      }
    }
  }
Exemplo n.º 3
0
  @Override
  public void postCreateFeatureType(
      SimpleFeatureType featureType, DatabaseMetaData metadata, String schemaName, Connection cx)
      throws SQLException {
    // figure out if the table has a spatial index and mark the feature type as so
    for (AttributeDescriptor ad : featureType.getAttributeDescriptors()) {
      if (!(ad instanceof GeometryDescriptor)) {
        continue;
      }

      GeometryDescriptor gd = (GeometryDescriptor) ad;
      String idxTableName = "idx_" + featureType.getTypeName() + "_" + gd.getLocalName();

      ResultSet rs =
          metadata.getTables(
              null,
              dataStore.escapeNamePattern(metadata, schemaName),
              dataStore.escapeNamePattern(metadata, idxTableName),
              new String[] {"TABLE"});
      try {
        if (rs.next()) {
          gd.getUserData().put(SPATIALITE_SPATIAL_INDEX, idxTableName);
        }
      } finally {
        dataStore.closeSafe(rs);
      }
    }
  }
Exemplo n.º 4
0
  private Integer getSRID(GeometryDescriptor gDescr) {
    Integer result = null;
    if (gDescr != null) result = (Integer) gDescr.getUserData().get(JDBCDataStore.JDBC_NATIVE_SRID);

    if (result == null) result = currentSRID;
    return result;
  }
Exemplo n.º 5
0
  /** Creates GEOMETRY_COLUMN registrations and spatial indexes for all geometry columns */
  @Override
  public void postCreateTable(String schemaName, SimpleFeatureType featureType, Connection cx)
      throws SQLException {
    schemaName = schemaName != null ? schemaName : "public";
    String tableName = featureType.getName().getLocalPart();

    Statement st = null;
    try {
      st = cx.createStatement();

      // register all geometry columns in the database
      for (AttributeDescriptor att : featureType.getAttributeDescriptors()) {
        if (att instanceof GeometryDescriptor) {
          GeometryDescriptor gd = (GeometryDescriptor) att;

          // lookup or reverse engineer the srid
          int srid = -1;
          if (gd.getUserData().get(JDBCDataStore.JDBC_NATIVE_SRID) != null) {
            srid = (Integer) gd.getUserData().get(JDBCDataStore.JDBC_NATIVE_SRID);
          } else if (gd.getCoordinateReferenceSystem() != null) {
            try {
              Integer result = CRS.lookupEpsgCode(gd.getCoordinateReferenceSystem(), true);
              if (result != null) srid = result;
            } catch (Exception e) {
              LOGGER.log(
                  Level.FINE,
                  "Error looking up the " + "epsg code for metadata " + "insertion, assuming -1",
                  e);
            }
          }

          // assume 2 dimensions, but ease future customisation
          int dimensions = 2;

          // grab the geometry type
          String geomType = CLASS_TO_TYPE_MAP.get(gd.getType().getBinding());
          if (geomType == null) geomType = "GEOMETRY";

          String sql = null;
          if (getVersion(cx).compareTo(V_2_0_0) >= 0) {
            // postgis 2 and up we don't muck with geometry_columns, we just alter the
            // type directly to set the geometry type and srid
            // setup the geometry type
            sql =
                "ALTER TABLE \""
                    + schemaName
                    + "\".\""
                    + tableName
                    + "\" "
                    + "ALTER COLUMN \""
                    + gd.getLocalName()
                    + "\" "
                    + "TYPE geometry ("
                    + geomType
                    + ", "
                    + srid
                    + ");";

            LOGGER.fine(sql);
            st.execute(sql);
          } else {
            // register the geometry type, first remove and eventual
            // leftover, then write out the real one
            sql =
                "DELETE FROM GEOMETRY_COLUMNS"
                    + " WHERE f_table_catalog=''" //
                    + " AND f_table_schema = '"
                    + schemaName
                    + "'" //
                    + " AND f_table_name = '"
                    + tableName
                    + "'" //
                    + " AND f_geometry_column = '"
                    + gd.getLocalName()
                    + "'";

            LOGGER.fine(sql);
            st.execute(sql);

            sql =
                "INSERT INTO GEOMETRY_COLUMNS VALUES (''," //
                    + "'"
                    + schemaName
                    + "'," //
                    + "'"
                    + tableName
                    + "'," //
                    + "'"
                    + gd.getLocalName()
                    + "'," //
                    + dimensions
                    + "," //
                    + srid
                    + "," //
                    + "'"
                    + geomType
                    + "')";
            LOGGER.fine(sql);
            st.execute(sql);
          }

          // add srid checks
          if (srid > -1) {
            sql =
                "ALTER TABLE " //
                    + "\""
                    + schemaName
                    + "\"" //
                    + "." //
                    + "\""
                    + tableName
                    + "\"" //
                    + " ADD CONSTRAINT \"enforce_srid_" //
                    + gd.getLocalName()
                    + "\"" //
                    + " CHECK (ST_SRID(" //
                    + "\""
                    + gd.getLocalName()
                    + "\"" //
                    + ") = "
                    + srid
                    + ")";
            LOGGER.fine(sql);
            st.execute(sql);
          }

          // add dimension checks
          sql =
              "ALTER TABLE " //
                  + "\""
                  + schemaName
                  + "\"" //
                  + "." //
                  + "\""
                  + tableName
                  + "\"" //
                  + " ADD CONSTRAINT \"enforce_dims_" //
                  + gd.getLocalName()
                  + "\"" //
                  + " CHECK (st_ndims(\""
                  + gd.getLocalName()
                  + "\")" //
                  + " = 2)";
          LOGGER.fine(sql);
          st.execute(sql);

          // add geometry type checks
          if (!geomType.equals("GEOMETRY")) {
            sql =
                "ALTER TABLE " //
                    + "\""
                    + schemaName
                    + "\"" //
                    + "." //
                    + "\""
                    + tableName
                    + "\"" //
                    + " ADD CONSTRAINT \"enforce_geotype_" //
                    + gd.getLocalName()
                    + "\"" //
                    + " CHECK (geometrytype(" //
                    + "\""
                    + gd.getLocalName()
                    + "\"" //
                    + ") = '"
                    + geomType
                    + "'::text "
                    + "OR \""
                    + gd.getLocalName()
                    + "\"" //
                    + " IS NULL)";
            LOGGER.fine(sql);
            st.execute(sql);
          }

          // add the spatial index
          sql =
              "CREATE INDEX \"spatial_"
                  + tableName //
                  + "_"
                  + gd.getLocalName().toLowerCase()
                  + "\"" //
                  + " ON " //
                  + "\""
                  + schemaName
                  + "\"" //
                  + "." //
                  + "\""
                  + tableName
                  + "\"" //
                  + " USING GIST (" //
                  + "\""
                  + gd.getLocalName()
                  + "\"" //
                  + ")";
          LOGGER.fine(sql);
          st.execute(sql);
        }
      }
      if (!cx.getAutoCommit()) {
        cx.commit();
      }
    } finally {
      dataStore.closeSafe(st);
    }
  }
Exemplo n.º 6
0
  @Override
  public void postCreateTable(String schemaName, SimpleFeatureType featureType, Connection cx)
      throws SQLException {
    String tableName = featureType.getName().getLocalPart();
    String sql;

    Statement st = null;
    try {
      st = cx.createStatement();

      // register all geometry columns in the database
      for (AttributeDescriptor att : featureType.getAttributeDescriptors()) {
        if (att instanceof GeometryDescriptor) {
          GeometryDescriptor gd = (GeometryDescriptor) att;

          // this class is already set right, continue
          if (gd.getType().getBinding() == Geometry.class) {
            continue;
          }

          // lookup or reverse engineer the srid
          int srid = -1;
          if (gd.getUserData().get(JDBCDataStore.JDBC_NATIVE_SRID) != null) {
            srid = (Integer) gd.getUserData().get(JDBCDataStore.JDBC_NATIVE_SRID);
          } else if (gd.getCoordinateReferenceSystem() != null) {
            try {
              Integer result = CRS.lookupEpsgCode(gd.getCoordinateReferenceSystem(), true);
              if (result != null) srid = result;
            } catch (Exception e) {
              LOGGER.log(
                  Level.FINE,
                  "Error looking up the " + "epsg code for metadata " + "insertion, assuming -1",
                  e);
            }
          }

          // grab the geometry type
          String geomType = CLASS_TO_TYPE_MAP.get(gd.getType().getBinding());
          if (geomType == null) geomType = "GEOMETRY";

          // alter the table to use the right type
          if (schemaName != null) {
            sql =
                "ALTER TABLE \""
                    + schemaName
                    + "\".\""
                    + tableName
                    + "\" ALTER COLUMN \""
                    + gd.getLocalName()
                    + "\" "
                    + geomType;
          } else {
            sql =
                "ALTER TABLE \""
                    + tableName
                    + "\" ALTER COLUMN \""
                    + gd.getLocalName()
                    + "\" "
                    + geomType;
          }
          if (srid != -1) {
            sql += " SRID " + srid;
          }
          st.execute(sql);
        }
      }
    } finally {
      dataStore.closeSafe(st);
    }
  }