Examples of ShapefileDataStore


Examples of org.geotools.data.shapefile.ShapefileDataStore

        System.out.println(s);
    }
 
  public void testStates() throws Exception {
    File shapeFile=TestData.file(this, "featureTypes/states.shp");   
    ShapefileDataStore ds=new ShapefileDataStore(shapeFile.toURL());
   
    final FeatureSource<SimpleFeatureType,SimpleFeature> fs = ds.getFeatureSource("states");
    final ReferencedEnvelope env = new ReferencedEnvelope(fs.getBounds(),WGS84);
   
    final WMSMapContext map = new WMSMapContext();
        map.setAreaOfInterest(env);
        map.setMapWidth(mapWidth);
View Full Code Here

Examples of org.geotools.data.shapefile.ShapefileDataStore

        FeatureStore<SimpleFeatureType, SimpleFeature> fstore = null;
        DataStore dstore = null;
        File file = null;
        try {
            file = new File(tempDir, schema.getTypeName() + ".shp");
            dstore = new ShapefileDataStore(file.toURL());
            dstore.createSchema(schema);
           
            fstore = (FeatureStore<SimpleFeatureType, SimpleFeature>) dstore.getFeatureSource(schema.getTypeName());
            fstore.addFeatures(collection);
        } catch (IOException ioe) {
View Full Code Here

Examples of org.geotools.data.shapefile.ShapefileDataStore

          SimpleFeatureType renamed = tb.buildFeatureType();
          c = new RetypingFeatureCollection(c, renamed);
        }

        FeatureStore<SimpleFeatureType, SimpleFeature> fstore = null;
        ShapefileDataStore dstore = null;
        try {
            // create attribute name mappings, to be compatible
            // with shapefile constraints:
            //  - geometry field is always named the_geom
            //  - field names have a max length of 10
            Map<String,String> attributeMappings=createAttributeMappings(c.getSchema());
            // wraps the original collection in a remapping wrapper
            FeatureCollection remapped=new RemappingFeatureCollection(c,attributeMappings);
            SimpleFeatureType remappedSchema=(SimpleFeatureType)remapped.getSchema();
            dstore = buildStore(tempDir, charset,  remappedSchema);
            fstore = (FeatureStore<SimpleFeatureType, SimpleFeature>) dstore.getFeatureSource();
            // we need retyping too, because the shapefile datastore
            // could have sorted fields in a different order
            FeatureCollection<SimpleFeatureType, SimpleFeature> retyped = new RetypingFeatureCollection(remapped, fstore.getSchema());
            fstore.addFeatures(retyped);
         
        } catch (IOException ioe) {
            LOGGER.log(Level.WARNING,
                "Error while writing featuretype '" + schema.getTypeName() + "' to shapefile.", ioe);
            throw new ServiceException(ioe);
        } finally {
            if(dstore != null) {
                dstore.dispose();
            }
        }
    }
View Full Code Here

Examples of org.geotools.data.shapefile.ShapefileDataStore

     */
    private ShapefileDataStore buildStore(File tempDir,
            Charset charset, SimpleFeatureType schema) throws MalformedURLException,
            FileNotFoundException, IOException {
        File file = new File(tempDir, schema.getTypeName() + ".shp");
        ShapefileDataStore sfds = new ShapefileDataStore(file.toURL());
       
        // handle shapefile encoding
        // and dump the charset into a .cst file, for debugging and control purposes
        // (.cst is not a standard extension)
        sfds.setStringCharset(charset);
        File charsetFile = new File(tempDir, schema.getTypeName()+ ".cst");
        PrintWriter pw = null;
        try {
            pw  = new PrintWriter(charsetFile);
            pw.write(charset.name());
        } finally {
            if(pw != null) pw.close();
        }

        try {
            sfds.createSchema(schema);
        } catch (NullPointerException e) {
            LOGGER.warning(
                "Error in shapefile schema. It is possible you don't have a geometry set in the output. \n"
                + "Please specify a <wfs:PropertyName>geom_column_name</wfs:PropertyName> in the request");
            throw new ServiceException(
                "Error in shapefile schema. It is possible you don't have a geometry set in the output.");
        }
       
        try {
            if(schema.getCoordinateReferenceSystem() != null)
                sfds.forceSchemaCRS(schema.getCoordinateReferenceSystem());
        } catch(Exception e) {
            LOGGER.log(Level.WARNING, "Could not properly create the .prj file", e);
        }

        return sfds;
View Full Code Here

Examples of org.geotools.data.shapefile.ShapefileDataStore

    }
    zis.close();
   
    // create a datastore reading the uncompressed shapefile
    File shapeFile = new File(shapeFileName);
    ShapefileDataStore ds = new ShapefileDataStore(shapeFile.toURL());
    FeatureSource<SimpleFeatureType, SimpleFeature> fs = ds
        .getFeatureSource();
    FeatureCollection<SimpleFeatureType, SimpleFeature> fc = fs
        .getFeatures();
    SimpleFeatureType schema = fc.getSchema();
   
View Full Code Here

Examples of org.geotools.data.shapefile.ShapefileDataStore

      Map<String, Serializable> params = new HashMap<String, Serializable>();
      params.put("url", outputFile.toURI().toURL());
      params.put("create spatial index", Boolean.TRUE);

      ShapefileDataStore newDataStore = (ShapefileDataStore) dataStoreFactory
          .createNewDataStore(params);
      newDataStore.setStringCharset(Charset.forName("UTF-8"));
      newDataStore.createSchema(features.get(0).getFeatureType());

      /*
       * You can comment out this line if you are using the
       * createFeatureType method (at end of class file) rather than
       * DataUtilities.createType
       */
      CoordinateReferenceSystem crs = features.get(0).getFeatureType()
          .getCoordinateReferenceSystem();
      if (crs == null)
        newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
     
      for (int i=1;i<features.size();i++) {
       
        SimpleFeature feature = features.get(i);
       
        CoordinateReferenceSystem crs2 = feature.getFeatureType()
        .getCoordinateReferenceSystem();
       
        if(crs2==null)
          continue;
       
        if(!crs.equals(crs2)) {
          remainingFeatures.add(feature);
          features.remove(i);
          i--;
        }
      }
      /*
       * Write the features to the shapefile
       */
      Transaction transaction = new DefaultTransaction("create");

      String typeName = newDataStore.getTypeNames()[0];
      SimpleFeatureSource featureSource = newDataStore
          .getFeatureSource(typeName);

      if (featureSource instanceof SimpleFeatureStore) {
        SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;

View Full Code Here

Examples of org.geotools.data.shapefile.ShapefileDataStore

    params.put("url", file.toURI().toURL());
    DataStore ds = DataStoreFinder.getDataStore(params);

    // Shapefile DBF's charset
    if (ds instanceof ShapefileDataStore && charsetName != null) {
      ShapefileDataStore shpDs = (ShapefileDataStore) ds;
      try {
        shpDs.setStringCharset(Charset.forName(charsetName));
      } catch (Exception ex) {
      }
    }
    return ds;
  }
View Full Code Here

Examples of org.geotools.data.shapefile.ShapefileDataStore

  public static void convertShapefile(GeometryStreamConverter converter, String filename, List<String> attributes) throws Exception
  {
    boolean debugTime = true;
    long startTime = System.currentTimeMillis();
   
    ShapefileDataStore dataStore = new ShapefileDataStore(new URL("file:///"+filename));
   
    try
    {
      convertFeatures(converter, dataStore, attributes);
    }
    finally
    {
      dataStore.dispose();
    }
   
    long endTime = System.currentTimeMillis();
    if (debugTime)
      System.out.println(String.format("file parsing took %s ms for %s", endTime - startTime, filename));
View Full Code Here

Examples of org.geotools.data.shapefile.ShapefileDataStore

              
                File file = new File(dir, name+".shp");
               
                System.out.println("Creating shapefile " + file.getAbsolutePath());
               
                ShapefileDataStore ds = new ShapefileDataStore(file.toURL());
                tb.init(proto);
                tb.setName(name);
                ds.createSchema(tb.buildFeatureType());
               
                FeatureWriter fw = ds.getFeatureWriterAppend(name, Transaction.AUTO_COMMIT);
                for (int j = 0; j < m; j++) {
                    fw.hasNext();
                    SimpleFeature f = (SimpleFeature)fw.next();
                    f.setDefaultGeometry(gfac.createPoint(new Coordinate(nextFloat(ran, -180, 180),
                        nextFloat(ran, -90, 90))));
View Full Code Here

Examples of org.geotools.data.shapefile.ShapefileDataStore

        Map<String, Serializable> params = new HashMap<String, Serializable>();
        params.put(ShapefileDataStoreFactory.URLP.key, file.toURI().toURL());
        params.put(ShapefileDataStoreFactory.CREATE_SPATIAL_INDEX.key, Boolean.FALSE);
        params.put(ShapefileDataStoreFactory.ENABLE_SPATIAL_INDEX.key, Boolean.FALSE);

        ShapefileDataStore dataStore = (ShapefileDataStore) dataStoreFactory
                .createNewDataStore(params);

        SimpleFeatureType outputFeatureType;
        try {
            outputFeatureType = getFeatureType(path, cli);
        } catch (GeoToolsOpException e) {
            cli.getConsole().println("No features to export.");
            return;
        }

        SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
        builder.add("geogig_fid", String.class);
        for (AttributeDescriptor descriptor : outputFeatureType.getAttributeDescriptors()) {
            builder.add(descriptor);
        }
        builder.setName(outputFeatureType.getName());
        builder.setCRS(outputFeatureType.getCoordinateReferenceSystem());
        outputFeatureType = builder.buildFeatureType();

        dataStore.createSchema(outputFeatureType);

        final String typeName = dataStore.getTypeNames()[0];
        final SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
        if (!(featureSource instanceof SimpleFeatureStore)) {
            throw new CommandFailedException("Could not create feature store.");
        }
        final SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;

        Function<Feature, Optional<Feature>> function = getTransformingFunction(dataStore
                .getSchema());

        ExportDiffOp op = cli.getGeogig().command(ExportDiffOp.class).setFeatureStore(featureStore)
                .setPath(path).setOldRef(commitOld).setNewRef(commitNew).setUseOld(old)
                .setTransactional(false).setFeatureTypeConversionFunction(function);
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.