@Override
  public Class<?> getMapping(ResultSet columnMetaData, Connection cx) throws SQLException {
    // the sqlite jdbc driver maps geometry type to varchar, so do a lookup
    // in the geometry_columns table
    String tbl = columnMetaData.getString("TABLE_NAME");
    String col = columnMetaData.getString("COLUMN_NAME");

    String sql =
        "SELECT type FROM geometry_columns "
            + "WHERE f_table_name = '"
            + tbl
            + "' "
            + "AND f_geometry_column = '"
            + col
            + "'";
    LOGGER.fine(sql);

    Statement st = cx.createStatement();
    try {
      ResultSet rs = st.executeQuery(sql);
      try {
        if (rs.next()) {
          String type = rs.getString("type");
          return Geometries.getForName(type).getBinding();
        }
      } finally {
        dataStore.closeSafe(rs);
      }

      // check geometry columns views
      sql =
          "SELECT b.type FROM views_geometry_columns a, geometry_columns b "
              + "WHERE a.f_table_name = b.f_table_name "
              + "AND a.f_geometry_column = b.f_geometry_column "
              + "AND a.view_name = '"
              + tbl
              + "' "
              + "AND a.view_geometry = '"
              + col
              + "'";
      LOGGER.fine(sql);
      try {
        rs = st.executeQuery(sql);
        try {
          if (rs.next()) {
            String type = rs.getString("type");
            return Geometries.getForName(type).getBinding();
          }
        } finally {
          dataStore.closeSafe(rs);
        }
      } catch (SQLException e) {
        LOGGER.log(Level.FINEST, "error querying views_geometry_columns", e);
      }
    } finally {
      dataStore.closeSafe(st);
    }

    return null;
  }
  @Override
  public void postCreateTable(String schemaName, SimpleFeatureType featureType, Connection cx)
      throws SQLException, IOException {

    // create any geometry columns entries after the fact
    for (AttributeDescriptor ad : featureType.getAttributeDescriptors()) {
      if (ad instanceof GeometryDescriptor) {
        GeometryDescriptor gd = (GeometryDescriptor) ad;
        StringBuffer sql = new StringBuffer("INSERT INTO geometry_columns VALUES (");

        // table name
        sql.append("'").append(featureType.getTypeName()).append("',");

        // geometry name
        sql.append("'").append(gd.getLocalName()).append("',");

        // type
        String gType =
            Geometries.getForBinding((Class<? extends Geometry>) gd.getType().getBinding())
                .getName();
        if (gType == null) {
          throw new IOException("Unknown geometry type: " + gd.getType().getBinding());
        }
        sql.append("'").append(gType).append("',");

        // coord dimension
        sql.append(2).append(",");

        // srid
        Integer epsgCode = null;
        if (gd.getCoordinateReferenceSystem() != null) {
          CoordinateReferenceSystem crs = gd.getCoordinateReferenceSystem();
          try {
            epsgCode = CRS.lookupEpsgCode(crs, true);
          } catch (Exception e) {
          }
        }
        if (epsgCode == null) {
          throw new IOException("Unable to find epsg code code.");
        }
        sql.append(epsgCode).append(",");

        // spatial index enabled
        sql.append(0).append(")");

        LOGGER.fine(sql.toString());
        Statement st = cx.createStatement();
        try {
          st.executeUpdate(sql.toString());
        } finally {
          dataStore.closeSafe(st);
        }
      }
    }
  }
Exemple #3
0
  @SuppressWarnings("unchecked")
  private static Style createStyle(SimpleFeatureSource source, String fieldName) throws Exception {

    StyleFactory styleFactory = CommonFactoryFinder.getStyleFactory();
    FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();

    Function colourFn = ff.function("colorlookup", ff.literal(source), ff.property(fieldName));

    Stroke stroke =
        styleFactory.createStroke(
            colourFn,
            ff.literal(1.0f), // line
            // width
            ff.literal(1.0f)); // opacity

    Fill fill = styleFactory.createFill(colourFn, ff.literal(1.0f)); // opacity

    Class<?> geomClass = source.getSchema().getGeometryDescriptor().getType().getBinding();
    Symbolizer sym = null;
    Geometries geomType = Geometries.getForBinding((Class<? extends Geometry>) geomClass);

    switch (geomType) {
      case POLYGON:
      case MULTIPOLYGON:
        sym = styleFactory.createPolygonSymbolizer(stroke, fill, null);
        break;

      case LINESTRING:
      case MULTILINESTRING:
        sym = styleFactory.createLineSymbolizer(stroke, null);
        break;

      case POINT:
      case MULTIPOINT:
        Graphic gr = styleFactory.createDefaultGraphic();
        gr.graphicalSymbols().clear();
        Mark mark = styleFactory.getCircleMark();
        mark.setFill(fill);
        mark.setStroke(stroke);
        gr.graphicalSymbols().add(mark);
        gr.setSize(ff.literal(10.0f));
        sym = styleFactory.createPointSymbolizer(gr, null);
        break;

      default:
        throw new IllegalArgumentException("Unsupported geometry type");
    }

    Style style = SLD.wrapSymbolizers(sym);

    return style;
  }
  /**
   * Create a new helper to work with a {@code MapLayer} that has vector feature data.
   *
   * @param layer the map layer
   * @param geomAttributeName the name of the geometry attribute for {@code Features}
   * @param geomClass the geometry class
   */
  public VectorLayerHelper(MapContext context, MapLayer layer) {
    super(context, layer.getFeatureSource().getSchema().getCoordinateReferenceSystem());

    this.layerRef = new WeakReference<MapLayer>(layer);

    final GeometryDescriptor geomDesc =
        layer.getFeatureSource().getSchema().getGeometryDescriptor();
    this.attrName = geomDesc.getLocalName();

    final Class<? extends Geometry> geomClass =
        (Class<? extends Geometry>) geomDesc.getType().getBinding();
    final Geometries type = Geometries.getForBinding(geomClass);
    this.isPolygonGeometry = (type == Geometries.POLYGON || type == Geometries.MULTIPOLYGON);
  }
  @Override
  public Class<?> getMapping(ResultSet columnMetaData, Connection cx) throws SQLException {

    String typeName = columnMetaData.getString("TYPE_NAME");
    if ("UUID".equalsIgnoreCase(typeName)) {
      return UUID.class;
    } else if ("BLOB".equalsIgnoreCase(typeName)) {
      String schemaName = columnMetaData.getString("TABLE_SCHEM");
      String tableName = columnMetaData.getString("TABLE_NAME");
      String columnName = columnMetaData.getString("COLUMN_NAME");

      // look up in geometry columns table
      StringBuffer sql = new StringBuffer("SELECT type FROM geometry_columns WHERE ");
      if (schemaName != null) {
        sql.append("f_table_schema = '").append(schemaName).append("'").append(" AND ");
      }
      sql.append("f_table_name = '").append(tableName).append("' AND ");
      sql.append("f_geometry_column = '").append(columnName).append("'");

      Statement st = cx.createStatement();
      try {
        LOGGER.fine(sql.toString());
        ResultSet rs = st.executeQuery(sql.toString());
        try {
          if (rs.next()) {
            String type = rs.getString(1);
            Geometries g = Geometries.getForName(type);
            if (g != null) {
              return g.getBinding();
            }
            LOGGER.warning("Geometry type " + type + " not supported.");
          }
        } finally {
          dataStore.closeSafe(rs);
        }
      } finally {
        dataStore.closeSafe(st);
      }

      // not a geometry blob, return byte[].class
      return byte[].class;
    }

    // do a check for a column remark which marks this as a geometry
    // do this mostly for backwards compatability
    String remark = columnMetaData.getString("REMARKS");
    if (remark != null) {
      Geometries g = Geometries.getForName(remark);
      if (g != null) {
        return g.getBinding();
      }
    }

    return null;
  }
  @Override
  public void postCreateTable(String schemaName, SimpleFeatureType featureType, Connection cx)
      throws SQLException {

    Statement st = cx.createStatement();
    String tableName = featureType.getTypeName();

    try {
      // post process the feature type and set up constraints based on geometry type
      for (PropertyDescriptor ad : featureType.getDescriptors()) {
        if (ad instanceof GeometryDescriptor) {
          GeometryDescriptor gd = (GeometryDescriptor) ad;
          Class binding = ad.getType().getBinding();
          String propertyName = ad.getName().getLocalPart();

          // create a spatial index
          int epsg = -1;
          try {
            CoordinateReferenceSystem crs = gd.getCoordinateReferenceSystem();
            if (crs == null) {
              continue;
            }

            epsg = CRS.lookupEpsgCode(crs, true);
          } catch (FactoryException e) {
            LOGGER.log(Level.FINER, "Unable to look epsg code", e);
          }

          StringBuffer sql = new StringBuffer();
          sql.append("CALL AddGeometryColumn(");
          if (schemaName != null) {
            sql.append("'").append(schemaName).append("'");
          } else {
            sql.append("NULL");
          }
          sql.append(", '").append(tableName).append("'");
          sql.append(", '").append(gd.getLocalName()).append("'");
          sql.append(", ").append(epsg);
          sql.append(", '").append(Geometries.getForBinding(binding).getName()).append("'");
          sql.append(", ").append(2); // TODO: dimension
          sql.append(")");

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

          if (epsg != -1) {
            sql = new StringBuffer();
            sql.append("CALL CreateSpatialIndex(");
            if (schemaName == null) {
              sql.append("NULL");
            } else {
              sql.append("'").append(schemaName).append("'");
            }

            sql.append(",'").append(tableName).append("'");
            sql.append(",'").append(propertyName).append("'");
            sql.append(",'").append(epsg).append("')");

            LOGGER.fine(sql.toString());
            st.execute(sql.toString());
          }
        }
      }
    } finally {
      dataStore.closeSafe(st);
    }
  }
 @Override
 public String getGeometryTypeName(Integer type) {
   return Geometries.getForSQLType(type).getName();
 }