use of org.geotoolkit.data.shapefile.shp.ShapefileReader in project geotoolkit by Geomatys.
the class IndexedShapefileFeatureStore method getEnvelope.
@Override
public org.opengis.geometry.Envelope getEnvelope(final Query query) throws DataStoreException {
if (!(query instanceof org.geotoolkit.storage.feature.query.Query))
throw new UnsupportedQueryException();
final org.geotoolkit.storage.feature.query.Query gquery = (org.geotoolkit.storage.feature.query.Query) query;
final Filter filter = gquery.getSelection();
if (filter == Filter.include() || QueryUtilities.queryAll(gquery)) {
// use the generic envelope calculation
return super.getEnvelope(gquery);
}
final Set<String> fids = new TreeSet<>();
IdCollectorFilterVisitor.ID_COLLECTOR.visit(filter, fids);
final Set records = new HashSet();
if (!fids.isEmpty()) {
Collection<ShpData> recordsFound = null;
try {
recordsFound = queryFidIndex(fids);
} catch (IOException ex) {
throw new DataStoreException(ex);
}
if (recordsFound != null) {
records.addAll(recordsFound);
}
}
if (records.isEmpty())
return null;
final AccessManager locker = shpFiles.createLocker();
ShapefileReader reader = null;
try {
reader = locker.getSHPReader(false, false, false, null);
final JTSEnvelope2D ret = new JTSEnvelope2D(FeatureExt.getCRS(getFeatureType(getNames().iterator().next().toString())));
for (final Iterator iter = records.iterator(); iter.hasNext(); ) {
final Data data = (Data) iter.next();
reader.goTo(((Long) data.getValue(1)).intValue());
final Record record = reader.nextRecord();
ret.expandToInclude(record.minX, record.minY);
ret.expandToInclude(record.maxX, record.maxY);
}
return ret;
} catch (IOException ex) {
throw new DataStoreException(ex);
} finally {
// todo replace by ARM in JDK 1.7
if (reader != null) {
try {
reader.close();
} catch (IOException ex) {
throw new DataStoreException(ex);
}
}
}
}
use of org.geotoolkit.data.shapefile.shp.ShapefileReader in project geotoolkit by Geomatys.
the class ShapeFileIndexer method index.
/**
* Index the shapefile denoted by setShapeFileName(String fileName) If when
* a thread starts, another thread is indexing the same file, this thread
* will wait that the first thread ends indexing; in this case <b>zero</b>
* is reurned as result of the indexing process.
*
* @param verbose
* enable/disable printing of dots every 500 indexed records
* @param listener
* DOCUMENT ME!
*
* @return The number of indexed records (or zero)
*
* @throws MalformedURLException
* @throws IOException
* @throws TreeException
* @throws StoreException
* DOCUMENT ME!
* @throws LockTimeoutException
*/
public int index(final boolean verbose, final ProgressController listener) throws MalformedURLException, IOException, TreeException, StoreException {
if (this.shpFiles == null) {
throw new IOException("You have to set a shape file name!");
}
int cnt = 0;
final AccessManager locker = shpFiles.createLocker();
try (Closeable disposeLocker = locker::disposeReaderAndWriters) {
// Temporary file for building...
final StorageFile storage = locker.getStorageFile(this.idxType.shpFileType);
final Path treeFile = storage.getFile();
try (ShapefileReader reader = locker.getSHPReader(true, false, false, null)) {
switch(idxType) {
case QIX:
cnt = this.buildQuadTree(locker, reader, treeFile, verbose);
break;
default:
throw new IllegalArgumentException("NONE is not a legal index choice");
}
} catch (DataStoreException ex) {
if (ex.getCause() instanceof IOException)
throw (IOException) ex.getCause();
else
throw new IOException(ex);
}
}
locker.replaceStorageFiles();
return cnt;
}
use of org.geotoolkit.data.shapefile.shp.ShapefileReader in project geotoolkit by Geomatys.
the class AccessManager method getSHPReader.
public ShapefileReader getSHPReader(final boolean strict, final boolean memoryMapped, final boolean read3D, final double[] resample) throws IOException, DataStoreException {
final URI shpUrl = files.getURI(ShpFileType.SHP);
final ReadableByteChannel shpChannel = toClosingChannel(files.getReadChannel(shpUrl), false);
final URI shxUrl = files.getURI(ShpFileType.SHX);
final ReadableByteChannel shxChannel;
if (shxUrl == null || (files.isWritable() && !files.exists(shxUrl))) {
// shx does not exist
shxChannel = null;
} else {
shxChannel = toClosingChannel(files.getReadChannel(shxUrl), false);
}
final ShapefileReader shpReader = new ShapefileReader(shpChannel, shxChannel, strict, memoryMapped, read3D, resample);
readEntries.add(new AccessEntry(ShpFileType.SHP, shpUrl, shpReader));
readEntries.add(new AccessEntry(ShpFileType.SHX, shxUrl, shpReader));
return shpReader;
}
use of org.geotoolkit.data.shapefile.shp.ShapefileReader in project geotoolkit by Geomatys.
the class ShapefileTest method testShapefileReaderRecord.
@Test
public void testShapefileReaderRecord() throws Exception {
final URL c1 = ShapeTestData.url(STATEPOP);
final ShpFiles shpFiles = new ShpFiles(c1);
final AccessManager locker = shpFiles.createLocker();
ShapefileReader reader = locker.getSHPReader(false, false, true, null);
URL c2;
try {
ArrayList offsets = new ArrayList();
while (reader.hasNext()) {
ShapefileReader.Record record = reader.nextRecord();
offsets.add(new Integer(record.offset()));
Geometry geom = (Geometry) record.shape();
assertEquals(new Envelope(record.minX, record.maxX, record.minY, record.maxY), geom.getEnvelopeInternal());
assertNotNull(record.toString());
}
copyShapefiles(STATEPOP);
reader.close();
c2 = TestData.url(AbstractTestCaseSupport.class, STATEPOP);
final ShpFiles shpFiles2 = new ShpFiles(c2);
final AccessManager locker2 = shpFiles.createLocker();
reader = locker.getSHPReader(false, false, true, null);
for (int i = 0, ii = offsets.size(); i < ii; i++) {
reader.shapeAt(((Integer) offsets.get(i)).intValue());
}
} finally {
reader.close();
}
}
use of org.geotoolkit.data.shapefile.shp.ShapefileReader in project geotoolkit by Geomatys.
the class ShapefileTest method testShapefileReaderRecord.
@Override
@Test
public void testShapefileReaderRecord() throws Exception {
final File file = copyShapefiles(STATEPOP);
final ShpFiles shpFiles = new ShpFiles(file.toURI().toURL());
final AccessManager locker = shpFiles.createLocker();
ShapefileReader reader = locker.getSHPReader(false, false, true, null);
ArrayList offsets = new ArrayList();
while (reader.hasNext()) {
ShapefileReader.Record record = reader.nextRecord();
offsets.add(new Integer(record.offset()));
Geometry geom = (Geometry) record.shape();
assertEquals(new Envelope(record.minX, record.maxX, record.minY, record.maxY), geom.getEnvelopeInternal());
assertNotNull(record.toString());
}
reader.close();
copyShapefiles(STATEPOP);
reader = locker.getSHPReader(false, false, true, null);
for (int i = 0, ii = offsets.size(); i < ii; i++) {
reader.shapeAt(((Integer) offsets.get(i)).intValue());
}
reader.close();
}
Aggregations