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

📄 indexedshapefiledatastore.java

📁 shape file read and write
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    // protected RTree openRTree() throws IOException {
    // if (!isLocal()) {
    // return null;
    // }
    // URL treeURL = shpFiles.acquireRead(GRX, this);
    // try {
    // File treeFile = DataUtilities.urlToFile(treeURL);
    //
    // if (!treeFile.exists() || (treeFile.length() == 0)) {
    // treeType = IndexType.NONE;
    // return null;
    // }
    //
    // try {
    // FileSystemPageStore fps = new FileSystemPageStore(treeFile);
    // rtree = new RTree(fps);
    // } catch (TreeException re) {
    // throw new DataSourceException("Error opening RTree", re);
    // }
    //
    // return rtree;
    // } finally {
    // shpFiles.unlockRead(treeURL, this);
    // }
    // }

    /**
     * Convenience method for opening a QuadTree index.
     * 
     * @return A new QuadTree
     * 
     * @throws StoreException
     */
    protected QuadTree openQuadTree() throws StoreException {
        if (!isLocal()) {
            return null;
        }
        URL treeURL = shpFiles.acquireRead(QIX, this);
        try {
            File treeFile = DataUtilities.urlToFile(treeURL);

            if (!treeFile.exists() || (treeFile.length() == 0)) {
                treeType = IndexType.NONE;
                return null;
            }

            try {
                FileSystemIndexStore store = new FileSystemIndexStore(treeFile);
                return store.load(openIndexFile());
            } catch (IOException e) {
                throw new StoreException(e);
            }
        } finally {
            shpFiles.unlockRead(treeURL, this);
        }

    }

    /**
     * Create a FeatureWriter for the given type name.
     * 
     * @param typeName
     *                The typeName of the FeatureType to write
     * @param transaction
     *                DOCUMENT ME!
     * 
     * @return A new FeatureWriter.
     * 
     * @throws IOException
     *                 If the typeName is not available or some other error
     *                 occurs.
     */
    protected FeatureWriter<SimpleFeatureType, SimpleFeature> createFeatureWriter(String typeName,
            Transaction transaction) throws IOException {
        typeCheck(typeName);

         FeatureReader<SimpleFeatureType, SimpleFeature> featureReader;
        IndexedShapefileAttributeReader attReader = getAttributesReader(true,
                true, null);
        try {
            SimpleFeatureType schema = getSchema();
            if (schema == null) {
                throw new IOException(
                        "To create a shapefile, you must first call createSchema()");
            }
            featureReader = createFeatureReader(typeName, attReader, schema);

        } catch (Exception e) {

            featureReader = new EmptyFeatureReader<SimpleFeatureType, SimpleFeature>(schema);
        }

        return new IndexedShapefileFeatureWriter(typeName, shpFiles, attReader,
                featureReader, this);
    }

    /**
     * @see org.geotools.data.AbstractDataStore#getBounds(org.geotools.data.Query)
     */
    protected ReferencedEnvelope getBounds(Query query) throws IOException {
        ReferencedEnvelope ret = null;

        Set records = new HashSet();
        Filter filter = query.getFilter();
        if (filter == Filter.INCLUDE || query == Query.ALL) {
            return getBounds();
        }
        // else if (this.useIndex) {
        // if (treeType == IndexType.GRX) {
        // return getBoundsRTree(query);
        // }
        // }

        Set<String> fids = (Set<String>) filter.accept(
                IdCollectorFilterVisitor.ID_COLLECTOR, new HashSet());

        if (!fids.isEmpty()) {
            Collection<Data> recordsFound = queryFidIndex(fids);
            if (recordsFound != null) {
                records.addAll(recordsFound);
            }
        }

        if (records.isEmpty())
            return null;

        ShapefileReader reader = new ShapefileReader(shpFiles, false, false);
        try {
            ret = new ReferencedEnvelope(getSchema().getCRS());
            for (Iterator iter = records.iterator(); iter.hasNext();) {
                Data data = (Data) iter.next();
                reader.goTo(((Long) data.getValue(1)).intValue());
                Record record = reader.nextRecord();
                ret.expandToInclude(new Envelope(record.minX, record.maxX,
                        record.minY, record.maxY));
            }
            return ret;
        } finally {
            reader.close();
        }
    }

    // private ReferencedEnvelope getBoundsRTree(Query query) throws IOException
    // {
    // ReferencedEnvelope ret = null;
    //
    // RTree rtree = this.openRTree();
    //
    // if (rtree != null) {
    // try {
    // Envelope envelopeFromIndex = rtree.getBounds(query.getFilter());
    // ret = new ReferencedEnvelope(envelopeFromIndex, schema.getCRS());
    // } catch (TreeException e) {
    // LOGGER.log(Level.SEVERE, e.getMessage(), e);
    // } catch (UnsupportedFilterException e) {
    // // Ignoring...
    // } finally {
    // try {
    // rtree.close();
    // } catch (Exception ee) {
    // }
    // }
    // }
    // return ret;
    // }

    /**
     * @see org.geotools.data.DataStore#getFeatureSource(java.lang.String)
     *
    public FeatureSource<SimpleFeatureType, SimpleFeature> getFeatureSource(final String typeName)
            throws IOException {
        final SimpleFeatureType featureType = getSchema(typeName);

        if (isWriteable) {
            if (getLockingManager() != null) {
                return new AbstractFeatureLocking() {
                    public DataStore getDataStore() {
                        return IndexedShapefileDataStore.this;
                    }

                    public void addFeatureListener(FeatureListener listener) {
                        listenerManager.addFeatureListener(this, listener);
                    }

                    public void removeFeatureListener(FeatureListener listener) {
                        listenerManager.removeFeatureListener(this, listener);
                    }

                    public SimpleFeatureType getSchema() {
                        return featureType;
                    }

                    public ReferencedEnvelope getBounds(Query query)
                            throws IOException {
                        return IndexedShapefileDataStore.this.getBounds(query);
                    }
                };
            } else {
                return new AbstractFeatureStore() {
                    public DataStore getDataStore() {
                        return IndexedShapefileDataStore.this;
                    }

                    public void addFeatureListener(FeatureListener listener) {
                        listenerManager.addFeatureListener(this, listener);
                    }

                    public void removeFeatureListener(FeatureListener listener) {
                        listenerManager.removeFeatureListener(this, listener);
                    }

                    public SimpleFeatureType getSchema() {
                        return featureType;
                    }

                    public ReferencedEnvelope getBounds(Query query)
                            throws IOException {
                        return IndexedShapefileDataStore.this.getBounds(query);
                    }
                };
            }
        } else {
            return new AbstractFeatureSource() {
                public DataStore getDataStore() {
                    return IndexedShapefileDataStore.this;
                }

                public void addFeatureListener(FeatureListener listener) {
                    listenerManager.addFeatureListener(this, listener);
                }

                public void removeFeatureListener(FeatureListener listener) {
                    listenerManager.removeFeatureListener(this, listener);
                }

                public SimpleFeatureType getSchema() {
                    return featureType;
                }

                public ReferencedEnvelope getBounds(Query query)
                        throws IOException {
                    return IndexedShapefileDataStore.this.getBounds(query);
                }
            };
        }
    }
    */

    //
    // /**
    // * Builds the RTree index
    // *
    // * @throws TreeException
    // * DOCUMENT ME!
    // */
    // void buildRTree() throws TreeException {
    // if (isLocal()) {
    // LOGGER.fine("Creating spatial index for " + shpFiles.get(SHP));
    //
    // synchronized (this) {
    // if (rtree != null) {
    // rtree.close();
    // }
    //
    // rtree = null;
    // }
    //
    // ShapeFileIndexer indexer = new ShapeFileIndexer();
    // indexer.setIdxType(IndexType.GRX);
    // indexer.setShapeFileName(shpFiles);
    //
    // try {
    // indexer.index(false, new NullProgressListener());
    // } catch (MalformedURLException e) {
    // throw new TreeException(e);
    // } catch (LockTimeoutException e) {
    // throw new TreeException(e);
    // } catch (Exception e) {
    // if (e instanceof TreeException) {
    // throw (TreeException) e;
    // } else {
    // throw new TreeException(e);
    // }
    // }
    // }
    // }

    /**
     * Builds the QuadTree index. Usually not necessary since reading features
     * will index when required
     * 
     * @param maxDepth
     *                depth of the tree. if < 0 then a best guess is made.
     * @throws TreeException
     */
    public void buildQuadTree(int maxDepth) throws TreeException {
        if (isLocal()) {
            LOGGER.fine("Creating spatial index for " + shpFiles.get(SHP));

            ShapeFileIndexer indexer = new ShapeFileIndexer();
            indexer.setIdxType(IndexType.QIX);
            indexer.setShapeFileName(shpFiles);
            indexer.setMax(maxDepth);

            try {
                indexer.index(false, new NullProgressListener());
            } catch (MalformedURLException e) {
                throw new TreeException(e);
            } catch (LockTimeoutException e) {
                throw new TreeException(e);
            } catch (Exception e) {
                if (e instanceof TreeException) {
                    throw (TreeException) e;
                } else {
                    throw new TreeException(e);
                }
            }
        }
    }

    public boolean isMemoryMapped() {
        return useMemoryMappedBuffer;
    }

    public String id() {
        return getClass().getName() + ": " + getCurrentTypeName();
    }
    
}

⌨️ 快捷键说明

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