/**
   * Forces the specified CRS on geometry attributes (all or some, depends on the parameters).
   *
   * @param schema the original schema
   * @param crs the forced crs
   * @param forceOnlyMissing if true, will force the specified crs only on the attributes that do
   *     miss one
   * @return
   * @throws SchemaException
   */
  public static SimpleFeatureType transform(
      SimpleFeatureType schema, CoordinateReferenceSystem crs, boolean forceOnlyMissing)
      throws SchemaException {
    SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();
    tb.setName(schema.getTypeName());
    tb.setNamespaceURI(schema.getName().getNamespaceURI());
    tb.setAbstract(schema.isAbstract());

    GeometryDescriptor defaultGeometryType = null;
    for (int i = 0; i < schema.getAttributeCount(); i++) {
      AttributeDescriptor attributeType = schema.getDescriptor(i);
      if (attributeType instanceof GeometryDescriptor) {
        GeometryDescriptor geometryType = (GeometryDescriptor) attributeType;
        AttributeDescriptor forced;

        tb.descriptor(geometryType);
        if (!forceOnlyMissing || geometryType.getCoordinateReferenceSystem() == null) {
          tb.crs(crs);
        }

        tb.add(geometryType.getLocalName(), geometryType.getType().getBinding());
      } else {
        tb.add(attributeType);
      }
    }
    if (schema.getGeometryDescriptor() != null) {
      tb.setDefaultGeometry(schema.getGeometryDescriptor().getLocalName());
    }

    tb.setSuperType((SimpleFeatureType) schema.getSuper());

    return tb.buildFeatureType();
  }
    public IntersectedFeatureCollection(
        SimpleFeatureCollection delegate,
        List<String> firstAttributes,
        SimpleFeatureCollection features,
        List<String> sndAttributes,
        IntersectionMode intersectionMode,
        boolean percentagesEnabled,
        boolean areasEnabled) {
      super(delegate);
      this.features = features;
      this.firstAttributes = firstAttributes;
      this.sndAttributes = sndAttributes;
      this.intersectionMode = intersectionMode;
      this.percentagesEnabled = percentagesEnabled;
      this.areasEnabled = areasEnabled;
      SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();

      SimpleFeatureType firstFeatureCollectionSchema = delegate.getSchema();
      SimpleFeatureType secondFeatureCollectionSchema = features.getSchema();

      if (intersectionMode == IntersectionMode.FIRST) {
        geomType = firstFeatureCollectionSchema.getGeometryDescriptor();
      }
      if (intersectionMode == IntersectionMode.SECOND) {
        geomType = secondFeatureCollectionSchema.getGeometryDescriptor();
      }
      if (intersectionMode == IntersectionMode.INTERSECTION) {
        geomType = getIntersectionType(delegate, features);
      }
      tb.add(geomType);

      // gather the attributes from the first feature collection and skip
      collectAttributes(firstFeatureCollectionSchema, firstAttributes, tb);
      // gather the attributes from the second feature collection
      collectAttributes(secondFeatureCollectionSchema, sndAttributes, tb);
      // add the dyamic attributes as needed
      if (percentagesEnabled) {
        tb.add("percentageA", Double.class);
        tb.add("percentageB", Double.class);
      }
      if (areasEnabled) {
        tb.add("areaA", Double.class);
        tb.add("areaB", Double.class);
      }
      tb.add("INTERSECTION_ID", Integer.class);
      tb.setDescription(firstFeatureCollectionSchema.getDescription());
      tb.setCRS(firstFeatureCollectionSchema.getCoordinateReferenceSystem());
      tb.setAbstract(firstFeatureCollectionSchema.isAbstract());
      tb.setSuperType((SimpleFeatureType) firstFeatureCollectionSchema.getSuper());
      tb.setName(firstFeatureCollectionSchema.getName());

      this.fb = new SimpleFeatureBuilder(tb.buildFeatureType());
    }
  /** Initializes the builder with state from a pre-existing feature type. */
  public void init(SimpleFeatureType type) {
    init();
    if (type == null) return;

    uri = type.getName().getNamespaceURI();
    local = type.getName().getLocalPart();
    description = type.getDescription();
    restrictions = null;
    restrictions().addAll(type.getRestrictions());
    this.defaultCrs = type.getCoordinateReferenceSystem();
    this.defaultCrsSet = true;
    attributes = null;
    attributes().addAll(type.getAttributeDescriptors());

    isAbstract = type.isAbstract();
    superType = (SimpleFeatureType) type.getSuper();
  }
示例#4
0
  /** Executes the export command using the provided options. */
  @Override
  protected final void runInternal(GeogigCLI cli) throws IOException {
    if (args.isEmpty()) {
      printUsage(cli);
      throw new CommandFailedException();
    }

    String path = args.get(0);
    String tableName = args.get(1);

    checkParameter(tableName != null && !tableName.isEmpty(), "No table name specified");

    DataStore dataStore = getDataStore();

    ObjectId featureTypeId = null;
    if (!Arrays.asList(dataStore.getTypeNames()).contains(tableName)) {
      SimpleFeatureType outputFeatureType;
      if (sFeatureTypeId != null) {
        // Check the feature type id string is a correct id
        Optional<ObjectId> id =
            cli.getGeogig().command(RevParse.class).setRefSpec(sFeatureTypeId).call();
        checkParameter(id.isPresent(), "Invalid feature type reference", sFeatureTypeId);
        TYPE type = cli.getGeogig().command(ResolveObjectType.class).setObjectId(id.get()).call();
        checkParameter(
            type.equals(TYPE.FEATURETYPE),
            "Provided reference does not resolve to a feature type: ",
            sFeatureTypeId);
        outputFeatureType =
            (SimpleFeatureType)
                cli.getGeogig()
                    .command(RevObjectParse.class)
                    .setObjectId(id.get())
                    .call(RevFeatureType.class)
                    .get()
                    .type();
        featureTypeId = id.get();
      } else {
        try {
          SimpleFeatureType sft = getFeatureType(path, cli);
          outputFeatureType =
              new SimpleFeatureTypeImpl(
                  new NameImpl(tableName),
                  sft.getAttributeDescriptors(),
                  sft.getGeometryDescriptor(),
                  sft.isAbstract(),
                  sft.getRestrictions(),
                  sft.getSuper(),
                  sft.getDescription());
        } catch (GeoToolsOpException e) {
          throw new CommandFailedException("No features to export.", e);
        }
      }
      try {
        dataStore.createSchema(outputFeatureType);
      } catch (IOException e) {
        throw new CommandFailedException("Cannot create new table in database", e);
      }
    } else {
      if (!overwrite) {
        throw new CommandFailedException("The selected table already exists. Use -o to overwrite");
      }
    }

    SimpleFeatureSource featureSource = dataStore.getFeatureSource(tableName);
    if (!(featureSource instanceof SimpleFeatureStore)) {
      throw new CommandFailedException("Can't write to the selected table");
    }
    SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
    if (overwrite) {
      try {
        featureStore.removeFeatures(Filter.INCLUDE);
      } catch (IOException e) {
        throw new CommandFailedException("Error truncating table: " + e.getMessage(), e);
      }
    }
    ExportOp op =
        cli.getGeogig()
            .command(ExportOp.class)
            .setFeatureStore(featureStore)
            .setPath(path)
            .setFilterFeatureTypeId(featureTypeId)
            .setAlter(alter);
    if (defaultType) {
      op.exportDefaultFeatureType();
    }
    try {
      op.setProgressListener(cli.getProgressListener()).call();
    } catch (IllegalArgumentException iae) {
      throw new org.locationtech.geogig.cli.InvalidParameterException(iae.getMessage(), iae);
    } catch (GeoToolsOpException e) {
      switch (e.statusCode) {
        case MIXED_FEATURE_TYPES:
          throw new CommandFailedException(
              "The selected tree contains mixed feature types. Use --defaulttype or --featuretype <feature_type_ref> to export.",
              e);
        default:
          throw new CommandFailedException("Could not export. Error:" + e.statusCode.name(), e);
      }
    }

    cli.getConsole().println(path + " exported successfully to " + tableName);
  }