Package org.geotools.data.shapefile.dbf

Examples of org.geotools.data.shapefile.dbf.DbaseFileReader$Row


    private ShpFiles shpFiles;

    @Before
    public void setUp() throws Exception {
        shpFiles = new ShpFiles(TestData.url(TEST_FILE));
        dbf = new DbaseFileReader(shpFiles, false,
                ShapefileDataStore.DEFAULT_STRING_CHARSET);
    }
View Full Code Here


    }

    @Test
    public void testRowVsEntry() throws Exception {
        Object[] attrs = new Object[dbf.getHeader().getNumFields()];
        DbaseFileReader dbf2 = new DbaseFileReader(shpFiles, false,
                ShapefileDataStore.DEFAULT_STRING_CHARSET);
        while (dbf.hasNext()) {
            dbf.readEntry(attrs);
            DbaseFileReader.Row r = dbf2.readRow();
            for (int i = 0, ii = attrs.length; i < ii; i++) {
                assertNotNull(attrs[i]);
                assertNotNull(r.read(i));
                assertEquals(attrs[i], r.read(i));
            }
        }
        dbf2.close();
    }
View Full Code Here

        for (int i = 0; i < header.getNumRecords(); i++) {
            dbf.write(new Object[6]);
        }
        dbf.close();
        ShpFiles tempShpFiles = new ShpFiles(f);
        DbaseFileReader r = new DbaseFileReader(tempShpFiles, false,
                ShapefileDataStore.DEFAULT_STRING_CHARSET);
        int cnt = 0;
        while (r.hasNext()) {
            cnt++;
            Object[] o = r.readEntry();
            assertTrue(o.length == r.getHeader().getNumFields());
        }
        assertEquals("Bad number of records", cnt, 20);
        r.close(); // make sure the channel is closed
        f.delete();
    }
View Full Code Here

        try {
            if (indexed) {
                return new IndexedDbaseFileReader(shpFiles, store.isMemoryMapped(),
                        store.getCharset(), store.getTimeZone());
            } else {
                return new DbaseFileReader(shpFiles, store.isMemoryMapped(), store.getCharset(),
                        store.getTimeZone());
            }
        } catch (IOException e) {
            // could happen if dbf file does not exist
            return null;
View Full Code Here

        }

        // setup the feature readers
        ShapefileSetManager shpManager = getDataStore().shpManager;
        ShapefileReader shapeReader = shpManager.openShapeReader(geometryFactory, goodRecs != null);
        DbaseFileReader dbfReader = null;
        List<AttributeDescriptor> attributes = readSchema.getAttributeDescriptors();
        if (attributes.size() < 1
                || (attributes.size() == 1 && readSchema.getGeometryDescriptor() != null)) {
            LOGGER.fine("The DBF file won't be opened since no attributes will be read from it");
        } else {
View Full Code Here

     */
    protected List<AttributeDescriptor> readAttributes() throws IOException {
        ShapefileSetManager shpManager = getDataStore().shpManager;
        PrjFileReader prj = null;
        ShapefileReader shp = null;
        DbaseFileReader dbf = null;
        CoordinateReferenceSystem crs = null;

        AttributeTypeBuilder build = new AttributeTypeBuilder();
        List<AttributeDescriptor> attributes = new ArrayList<AttributeDescriptor>();
        try {
            shp = shpManager.openShapeReader(new GeometryFactory(), false);
            dbf = shpManager.openDbfReader(false);
            try {
                prj = shpManager.openPrjReader();

                if (prj != null) {
                    crs = prj.getCoordinateReferenceSystem();
                }
            } catch (FactoryException fe) {
                crs = null;
            }
           
            Class<?> geometryClass = JTSUtilities.findBestGeometryClass(shp.getHeader()
                    .getShapeType());
            build.setName(Classes.getShortName(geometryClass));
            build.setNillable(true);
            build.setCRS(crs);
            build.setBinding(geometryClass);

            GeometryType geometryType = build.buildGeometryType();
            attributes.add(build.buildDescriptor(BasicFeatureTypes.GEOMETRY_ATTRIBUTE_NAME,
                    geometryType));
            Set<String> usedNames = new HashSet<String>(); // record names in
            // case of
            // duplicates
            usedNames.add(BasicFeatureTypes.GEOMETRY_ATTRIBUTE_NAME);

            // take care of the case where no dbf and query wants all =>
            // geometry only
            if (dbf != null) {
                DbaseFileHeader header = dbf.getHeader();
                for (int i = 0, ii = header.getNumFields(); i < ii; i++) {
                    Class attributeClass = header.getFieldClass(i);
                    String name = header.getFieldName(i);
                    if (usedNames.contains(name)) {
                        String origional = name;
                        int count = 1;
                        name = name + count;
                        while (usedNames.contains(name)) {
                            count++;
                            name = origional + count;
                        }
                        build.addUserData(ShapefileDataStore.ORIGINAL_FIELD_NAME, origional);
                        build.addUserData(ShapefileDataStore.ORIGINAL_FIELD_DUPLICITY_COUNT, count);
                    }
                    usedNames.add(name);
                    int length = header.getFieldLength(i);

                    build.setNillable(true);
                    build.setLength(length);
                    build.setBinding(attributeClass);
                    attributes.add(build.buildDescriptor(name));
                }
            }
            return attributes;
        } finally {

            try {
                if (prj != null) {
                    prj.close();
                }
            } catch (IOException ioe) {
                // do nothing
            }
            try {
                if (dbf != null) {
                    dbf.close();
                }
            } catch (IOException ioe) {
                // do nothing
            }
            try {
View Full Code Here

    try {
            Class geometryClass = JTSUtilities.findBestGeometryClass(shpReader.getHeader().getShapeType());
            Integer geometryType = SpatialDatabaseService.convertJtsClassToGeometryType(geometryClass);
     
      // TODO ask charset to user?
      DbaseFileReader dbfReader = new DbaseFileReader(shpFiles, shpMemoryMapped, charset);
      try {
        DbaseFileHeader dbaseFileHeader = dbfReader.getHeader();
             
        String[] fieldsName = new String[dbaseFileHeader.getNumFields()+1];
        fieldsName[0] = "ID";
        for (int i = 1; i < fieldsName.length; i++) {
          fieldsName[i] = dbaseFileHeader.getFieldName(i-1);
        }
       
        Transaction tx = database.beginTx();
        try {
          if (crs != null) {
            layer.setCoordinateReferenceSystem(crs);
          }

          if (geometryType != null) {
            layer.setGeometryType(geometryType);
          }
         
          layer.mergeExtraPropertyNames(fieldsName);
          tx.success();
        } finally {
          tx.close();
        }
       
        monitor.begin(dbaseFileHeader.getNumRecords());
        try {
          Record record;
          Geometry geometry;
          Object[] values;
                    ArrayList<Object> fields = new ArrayList<Object>();
          int recordCounter = 0;
          int filterCounter = 0;
          while (shpReader.hasNext() && dbfReader.hasNext()) {
            tx = database.beginTx();
            try {
              int committedSinceLastNotification = 0;
              for (int i = 0; i < commitInterval; i++) {
                if (shpReader.hasNext() && dbfReader.hasNext()) {
                  record = shpReader.nextRecord();
                  recordCounter++;
                  committedSinceLastNotification++;
                  try {
                                        fields.clear();
                    geometry = (Geometry) record.shape();
                    if (filterEnvelope == null || filterEnvelope.intersects(geometry.getEnvelopeInternal())) {
                      values = dbfReader.readEntry();
                      fields.add(recordCounter);
                      Collections.addAll(fields, values);
                      if (geometry.isEmpty()) {
                        log("warn | found empty geometry in record " + recordCounter);
                      } else {
                        // TODO check geometry.isValid()
                        // ?
                        layer.add(geometry, fieldsName, fields.toArray(values));
                      }
                    } else {
                      filterCounter ++;
                    }
                  } catch (IllegalArgumentException e) {
                    // org.geotools.data.shapefile.shp.ShapefileReader.Record.shape() can throw this exception
                    log("warn | found invalid geometry: index=" + recordCounter, e);         
                  }
                }
              }
              monitor.worked(committedSinceLastNotification);
              tx.success();

              log("info | inserted geometries: " + (recordCounter-filterCounter));
              if (filterCounter > 0) {
                log("info | ignored " + filterCounter + "/" + recordCounter
                    + " geometries outside filter envelope: " + filterEnvelope);
              }

            } finally {
              tx.close();
            }
          }
        } finally {
          monitor.done();
        }
      } finally {
        dbfReader.close();
      }     
    } finally {
      shpReader.close();
    }
View Full Code Here

TOP

Related Classes of org.geotools.data.shapefile.dbf.DbaseFileReader$Row

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.