Package org.geotools.data.shapefile.dbf

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


   * @return A list of attribute names in the DBF file
   */
  public static List<String> getAttributeNames(File dbfFile) throws IOException
  {
    FileInputStream fis = new FileInputStream(dbfFile);
    DbaseFileReader dbfReader = new DbaseFileReader(fis.getChannel(), false, Charset.forName("ISO-8859-1"));
   
    //contains the header columns
    DbaseFileHeader dbfHeader = dbfReader.getHeader();
   
    // get the names from the header
    List<String> names = new Vector<String>();
    int n = dbfHeader.getNumFields();
    for (int i = 0; i < n; i++)
View Full Code Here


   */
  public static Object[][] getDBFData(File dbfFile, String[] fieldNames) throws IOException
  {
    List<String> allFields = getAttributeNames(dbfFile);
    FileInputStream fis = new FileInputStream(dbfFile);
    DbaseFileReader dbfReader = new DbaseFileReader(fis.getChannel(), false, Charset.forName("ISO-8859-1"));
   
    //contains the header columns
    DbaseFileHeader dbfHeader = dbfReader.getHeader();
   
    List<Object[]> rowsList = new Vector<Object[]>();
   

    while(dbfReader.hasNext())
    {
      Object[] row;
      if (fieldNames != null)
      {
        dbfReader.read();
        row = new Object[fieldNames.length];
        for (int i = 0; i < fieldNames.length; i++)
        {
          int index = allFields.indexOf(fieldNames[i]);
          if (index < 0)
            row[i] = "";
          else
            row[i] = dbfReader.readField(index);
        }
      }
      else
      {
        row = dbfReader.readEntry();
      }
      rowsList.add(row);
    }
   
   
View Full Code Here

   
    // open each file, read each header, get the complete list of field names and types
    for (int i = 0; i < dbfFiles.length; i++)
    {
      inputStreams[i] = new FileInputStream(dbfFiles[i]);
      readers[i] = new DbaseFileReader(inputStreams[i].getChannel(), false, Charset.forName("ISO-8859-1"));
      headers[i] = readers[i].getHeader();
     
      int numFields = headers[i].getNumFields();
      // keep track of the full set of field names
      for (int col = 0; col < numFields; col++)
View Full Code Here

            writer.write(current);
        }
        writer.close();
        fos.flush();
        fos.close();
        DbaseFileReader reader = new DbaseFileReader(new FileInputStream(tmp).getChannel(), false, cs, tz);
        assertTrue("Number of records does not match", values.length == reader.getHeader().getNumRecords());
        for (int row = 0; row < values.length; row++) {
            Object[] current = reader.readEntry();
            assertTrue("Number of columns incorrect", current != null && current.length == values.length);
            for (int column = 0; column < values.length; column++) {
                if (column == row) {
                    assertTrue("Column was null and should not have been", current[column] != null);
                    assertTrue("Non-null column value " + current[column] +
View Full Code Here

    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

TOP

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

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.