⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shapefiledatastoretest.java

📁 shape file read and write
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        ds.createSchema(featureType);
        
        assertEquals("test", ds.getSchema().getTypeName());

        CoordinateReferenceSystem crs2 = ds.getSchema().getDefaultGeometry().getCRS();
        assertNotNull( crs2 );
        assertEquals( crs.getName(), crs2.getName() );
        
        file.deleteOnExit();
        file = new File("test.dbf");
        file.deleteOnExit();
        file = new File("test.shp");
        file.deleteOnExit();

        file = new File("test.prj");
        if (file.exists())
            file.deleteOnExit();

        file = new File("test.shx");
        if (file.exists()){
            file.deleteOnExit();
        }
        
        file = new File("test.prj");
        if( file.exists()){
            file.deleteOnExit();
        }
    }
    
    public void testForceCRS() throws Exception {
        File file = new File("test.shp");
        URL toURL = file.toURL();

        ShapefileDataStore ds = new ShapefileDataStore(toURL);
        ds.createSchema(DataUtilities.createType("test", "geom:MultiPolygon"));
        FeatureType before = ds.getSchema();

        ds.forceSchemaCRS(CRS.decode("EPSG:3005"));
        FeatureType after = ds.getSchema();

        assertNotSame(before, after);
        assertNull("4326", before.getCRS());
        assertEquals("NAD83 / BC Albers", after.getCRS().getName().getCode());

        file.deleteOnExit();
        file = new File("test.dbf");
        file.deleteOnExit();
        file = new File("test.shp");
        file.deleteOnExit();

        file = new File("test.prj");

        if (file.exists())
            file.deleteOnExit();

        file = new File("test.shx");
        if (file.exists())
            file.deleteOnExit();
    }

    private ShapefileDataStore createDataStore(File f) throws Exception {
        FeatureCollection<SimpleFeatureType, SimpleFeature> fc = createFeatureCollection();
        ShapefileDataStore sds = new ShapefileDataStore(f.toURL());
        writeFeatures(sds, fc);
        return sds;
    }

    private ShapefileDataStore createDataStore() throws Exception {
        return createDataStore(getTempFile());
    }

    /**
     * Create a set of features, then remove every other one, updating the
     * remaining. Test for removal and proper update after reloading...
     */
    public void testUpdating() throws Throwable {
        try {
            ShapefileDataStore sds = createDataStore();
            loadFeatures(sds);

            FeatureWriter<SimpleFeatureType, SimpleFeature> writer = null;
            try {
                writer = sds.getFeatureWriter(sds.getTypeNames()[0],
                        Filter.INCLUDE, Transaction.AUTO_COMMIT);
                while (writer.hasNext()) {
                    SimpleFeature feat = writer.next();
                    Byte b = (Byte) feat.getAttribute(1);
                    if (b.byteValue() % 2 == 0) {
                        writer.remove();
                    } else {
                        feat.setAttribute(1, new Byte((byte) -1));
                    }
                }
            } finally {
                if (writer != null)
                    writer.close();
            }
            FeatureCollection<SimpleFeatureType, SimpleFeature> fc = loadFeatures(sds);

            assertEquals(10, fc.size());
            for (FeatureIterator<SimpleFeature> i = fc.features(); i.hasNext();) {
                assertEquals(-1, ((Byte) i.next().getAttribute(1)).byteValue());
            }
        } catch (Throwable t) {
            if (System.getProperty("os.name").startsWith("Windows")) {
                System.out.println("Ignore " + t
                        + " because you are on windows");
                return;
            } else {
                throw t;
            }
        }
    }

    /**
     * Create a test file, then continue removing the first entry until there
     * are no features left.
     */
    public void testRemoveFromFrontAndClose() throws Throwable {
        try {
            ShapefileDataStore sds = createDataStore();

            int idx = loadFeatures(sds).size();

            while (idx > 0) {
                FeatureWriter<SimpleFeatureType, SimpleFeature> writer = null;

                try {
                    writer = sds.getFeatureWriter(sds.getTypeNames()[0],
                            Filter.INCLUDE, Transaction.AUTO_COMMIT);
                    writer.next();
                    writer.remove();
                } finally {
                    if (writer != null) {
                        writer.close();
                        writer = null;
                    }
                }
                assertEquals(--idx, loadFeatures(sds).size());
            }
        } catch (Throwable t) {
            if (System.getProperty("os.name").startsWith("Windows")) {
                System.out.println("Ignore " + t
                        + " because you are on windows");
                return;
            } else {
                throw t;
            }
        }

    }

    /**
     * Create a test file, then continue removing the last entry until there are
     * no features left.
     */
    public void testRemoveFromBackAndClose() throws Throwable {
        try {
            ShapefileDataStore sds = createDataStore();

            int idx = loadFeatures(sds).size();

            while (idx > 0) {
                FeatureWriter<SimpleFeatureType, SimpleFeature> writer = null;
                try {
                    writer = sds.getFeatureWriter(sds.getTypeNames()[0],
                            Filter.INCLUDE, Transaction.AUTO_COMMIT);
                    while (writer.hasNext()) {
                        writer.next();
                    }
                    writer.remove();
                } finally {
                    if (writer != null) {
                        writer.close();
                        writer = null;
                    }
                }
                assertEquals(--idx, loadFeatures(sds).size());
            }
        } catch (Throwable t) {
            if (System.getProperty("os.name").startsWith("Windows")) {
                System.out.println("Ignore " + t
                        + " because you are on windows");
                return;
            } else {
                throw t;
            }
        }
    }

    public void testWriteShapefileWithNoRecords() throws Exception {
        SimpleFeatureType featureType = DataUtilities.createType("whatever",
                "a:Polygon,b:String");

        File tempFile = getTempFile();
        ShapefileDataStore shapefileDataStore = new ShapefileDataStore(tempFile
                .toURL());
        shapefileDataStore.createSchema(featureType);

        FeatureWriter<SimpleFeatureType, SimpleFeature> featureWriter = shapefileDataStore.getFeatureWriter(
                shapefileDataStore.getTypeNames()[0], Transaction.AUTO_COMMIT);

        // don't add any features to the data store....

        // this should create a shapefile with no records. Not sure about the
        // semantics of this,
        // but it's meant to be used in the context of a FeatureCollection
        // iteration,
        // where the FeatureCollection<SimpleFeatureType, SimpleFeature> has nothing in it.
        featureWriter.close();
    }

    /**
     * Creates feature collection with all the stuff we care about from simple
     * types, to Geometry and date.
     * <p>
     * As we care about supporting more stuff please add on to the end of this
     * list...
     * 
     * @return FeatureCollection<SimpleFeatureType, SimpleFeature> For use in testing.
     * @throws Exception
     */
    private FeatureCollection<SimpleFeatureType, SimpleFeature> createFeatureCollection() throws Exception {
        SimpleFeatureType featureType = createExampleSchema();
        SimpleFeatureBuilder build = new SimpleFeatureBuilder(featureType);

        FeatureCollection<SimpleFeatureType, SimpleFeature> features = FeatureCollections.newCollection();
        for (int i = 0, ii = 20; i < ii; i++) {

            build.add(new GeometryFactory().createPoint(new Coordinate(1, -1)));
            build.add(new Byte((byte) i));
            build.add(new Short((short) i));
            build.add(new Double(i));
            build.add(new Float(i));
            build.add(new String(i + " "));
            build.add(new Date(i));
            build.add(new Boolean(true));
            build.add(new Integer(22));
            build.add(new Long(1234567890123456789L));
            build.add(new BigDecimal(new BigInteger(
                    "12345678901234567890123456789"), 2));
            build.add(new BigInteger("12345678901234567890123456789"));

            SimpleFeature feature = build.buildFeature(null);
            features.add(feature);
        }
        return features;
    }

    private SimpleFeatureType createExampleSchema() {
        SimpleFeatureTypeBuilder build = new SimpleFeatureTypeBuilder();
        build.setName("junk");
        build.add("a", Point.class);
        build.add("b", Byte.class);
        build.add("c", Short.class);
        build.add("d", Double.class);
        build.add("e", Float.class);
        build.add("f", String.class);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -