Package org.geotools.data.shapefile

Examples of org.geotools.data.shapefile.ShapefileDataStore


    /**
     * Test count versus old DataSource
     */
    public void testOptimizedCount() throws Exception {
        URL url = TestData.url(STATE_POP);
        ShapefileDataStore sds = new ShapefileDataStore(url);
        OGRDataStore s = new OGRDataStore(getAbsolutePath(STATE_POP), null, null, ogr);
        String typeName = s.getTypeNames()[0];

        assertEquals(sds.getCount(Query.ALL), s.getFeatureSource(typeName).getCount(Query.ALL));
    }
View Full Code Here


        assertEquals(schema.getSuper(), BasicFeatureTypes.POLYGON);
    }

    public void testShapefileComparison() throws Exception {
        URL url = TestData.url(STATE_POP);
        ShapefileDataStore sds = new ShapefileDataStore(url);
        OGRDataStore ods = new OGRDataStore(getAbsolutePath(STATE_POP), null, null, ogr);

        assertFeatureTypeEquals(sds.getSchema(), ods.getSchema(sds.getSchema().getTypeName()));

        Query query = new Query(sds.getSchema().getTypeName());
        FeatureReader sfr = sds.getFeatureReader(query, Transaction.AUTO_COMMIT);
        FeatureReader ofr = ods.getFeatureReader(query, Transaction.AUTO_COMMIT);
        SimpleFeature sf = null;
        SimpleFeature of = null;
        while (true) {
            if (!sfr.hasNext()) {
                assertTrue(!ofr.hasNext());
                break;
            }
            sf = (SimpleFeature) sfr.next();
            of = (SimpleFeature) ofr.next();
            for (int i = 0; i < sds.getSchema().getAttributeCount(); i++) {
                Object shapeAtt = sf.getAttribute(i);
                Object ogrAtt = of.getAttribute(i);
                assertEquals(shapeAtt, ogrAtt);
            }
        }
        sfr.close();
        ofr.close();
        sds.dispose();
        ods.dispose();
    }
View Full Code Here

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

     
    File directory = IOUtils.createTempDirectory("sxttmp");
        File file = new File(directory, m_sFilename);

        try {
            ShapefileDataStore dataStore = new ShapefileDataStore(DataUtilities.fileToURL(file));
            dataStore.createSchema(m_FeatureType);
            manager.addResource(new ShapefileResource(dataStore, directory));
           
            return dataStore;
        } catch(Throwable t) {
            LOGGER.log(Level.SEVERE, "Could not create shapefile output ", t);
View Full Code Here

    final DataStoreFactorySpi dataStoreFactory = new ShapefileDataStoreFactory();
    Map<String, Serializable> params = new HashMap<String, Serializable>();
    params.put("url", destination.toURI().toURL());
    params.put("create spatial index", Boolean.TRUE);
 
    ShapefileDataStore store=null;
    Transaction transaction=null;
    try{
      store = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
      store.createSchema(fc.getSchema());
     
      final SimpleFeatureStore featureStore =(SimpleFeatureStore) store.getFeatureSource(fc.getSchema().getName());
      transaction=featureStore.getTransaction();
     
      featureStore.addFeatures(fc);   
    }catch (IOException e) {
      e.printStackTrace();
    }finally{
     
      if(transaction!=null){
 
        transaction.commit();
        transaction.close()
      }
     
      if(store!=null){
        store.dispose();
      }
    }
  }
View Full Code Here

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

        SimpleFeatureStore 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
            SimpleFeatureCollection remapped = new RemappingFeatureCollection(c,attributeMappings);
            SimpleFeatureType remappedSchema=(SimpleFeatureType)remapped.getSchema();
            dstore = buildStore(tempDir, charset,  remappedSchema);
            fstore = (SimpleFeatureStore) dstore.getFeatureSource();
            // we need retyping too, because the shapefile datastore
            // could have sorted fields in a different order
            SimpleFeatureCollection retyped = new RetypingFeatureCollection(remapped, fstore.getSchema());
            fstore.addFeatures(retyped);
           
            changeWKTFormatIfFileFormatIsESRI(tempDir, request, fileName,
          remappedSchema);
         
        } catch (FactoryException fe) {
          LOGGER.log(Level.WARNING,
              "Error while getting EPSG code from FeatureType", fe);
          throw new ServiceException(fe);
        } 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

     */
    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

        if (shapeFile == null) {
            FileUtils.deleteDirectory(tempDir);
            throw new IOException("Could not find any file with .shp extension in the zip file");
        } else {
            ShapefileDataStore store = new ShapefileDataStore(DataUtilities.fileToURL(shapeFile));
            resources.addResource(new ShapefileResource(store, tempDir));
            return store.getFeatureSource().getFeatures();
        }

    }
View Full Code Here

        Map<String, Serializable> create = new HashMap<String, Serializable>();
        URL url = file.toURI().toURL();
        create.put("url", url);
        create.put("create spatial index", Boolean.TRUE);
        create.put("charset", "UTF-8");
        ShapefileDataStore shpDataStore = (ShapefileDataStore) factory.createNewDataStore(create);
        CoordinateReferenceSystem crs = null;
    try (Transaction tx = neo4jDataStore.beginTx()) {
      SimpleFeatureType featureType = neo4jDataStore.getSchema(layerName);
            GeometryDescriptor geometryType = featureType.getGeometryDescriptor();
            crs = geometryType.getCoordinateReferenceSystem();
            // crs = neo4jDataStore.getFeatureSource(layerName).getInfo().getCRS();

            shpDataStore.createSchema(featureType);
            FeatureStore store = (FeatureStore) shpDataStore.getFeatureSource();
            store.addFeatures(neo4jDataStore.getFeatureSource(layerName).getFeatures());
            tx.success();
    }
        if (crs != null)
            shpDataStore.forceSchemaCRS(crs);
        if (!file.exists()) {
            throw new Exception("Shapefile was not created: " + file);
        } else if (file.length() < 10) {
            throw new Exception("Shapefile was unexpectedly small, only " + file.length() + " bytes: " + file);
        }
View Full Code Here

        }
        zis.close();

        // create a datastore reading the uncompressed shapefile
        File shapeFile = new File(shapeFileName);
        ShapefileDataStore ds = new ShapefileDataStore(shapeFile.toURL());
        SimpleFeatureSource fs = ds.getFeatureSource();
        SimpleFeatureCollection fc = fs.getFeatures();
        SimpleFeatureType schema = fc.getSchema();

        SimpleFeatureIterator iter = fc.features();
        try {
View Full Code Here

TOP

Related Classes of org.geotools.data.shapefile.ShapefileDataStore

Copyright © 2018 www.massapicom. 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.