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

📄 queryshapefile.java

📁 使用ArcEngine java api开发的
💻 JAVA
字号:

/*
 * ArcGIS Engine Developer Sample
 * Application Name: QueryShapefile.java
 */
//package com.esri.arcgis.samples.geodatabase.accessingdata;

import com.esri.arcgis.datasourcesfile.ShapefileWorkspaceFactory;
import com.esri.arcgis.geodatabase.IFeature;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.IFeatureClassProxy;
import com.esri.arcgis.geodatabase.IFeatureCursor;
import com.esri.arcgis.geodatabase.IFeatureWorkspace;
import com.esri.arcgis.geodatabase.IFeatureWorkspaceProxy;
import com.esri.arcgis.geodatabase.IField;
import com.esri.arcgis.geodatabase.IFields;
import com.esri.arcgis.geodatabase.IWorkspace;
import com.esri.arcgis.geodatabase.IWorkspaceFactory;
import com.esri.arcgis.geodatabase.esriFieldType;
import com.esri.arcgis.system.EngineInitializer;

/**
 * Description:  This sample opens a cursor on a shapefile and prints the values of each field
 * for all the records in the shapefile.
 *
 * @param        folderName - a directory path on disk where the source shapefile exists
 * @param        shapeName - the name of the source shapefile
 */
public class QueryShapefile {

  /**
   * Opens a cursor on a shapefile and prints the values of each field
   * @param        folderName - a directory path on disk where the source shapefile exists
   * @param        shapeName - the name of the source shapefile
   */
  public void viewRecordsInShapefile(String folderName, String shapeName) {
    String dataPath = folderName;
    String featureClassName = shapeName;
    try {
      IWorkspaceFactory workspaceFactory = null;
      workspaceFactory = new ShapefileWorkspaceFactory();

      IWorkspace workspace = workspaceFactory.openFromFile(dataPath, 0);
      IFeatureWorkspace fWorkspace = new IFeatureWorkspaceProxy(workspace);
      IFeatureClass featureClass = new IFeatureClassProxy(fWorkspace.
          openFeatureClass(
          featureClassName));
      IFeatureCursor fCursur = featureClass.search(null, true);
      IFields fields = fCursur.getFields();
      int count = fields.getFieldCount();
      System.out.println("");
      IField field;
      String fieldName;
      for (int i = 0; i < count; i++) {
        field = fields.getField(i);
        fieldName = field.getName();
        System.out.print("  " + fieldName);
      }
      System.out.println("");
      String fieldValue = null;
      IFeature feature;

      feature = fCursur.nextFeature();
      while (feature != null) {
        fieldValue = null;
        for (int i = 0; i < count; i++) {
          int fieldType = feature.getFields().getField(i).getType();
          switch (fieldType) {
            case esriFieldType.esriFieldTypeSmallInteger:
              fieldValue = fieldValue + feature.getValue(i).toString() + " ";
              break;
            case esriFieldType.esriFieldTypeInteger:
              fieldValue = fieldValue + feature.getValue(i).toString() + " ";
              break;
            case esriFieldType.esriFieldTypeSingle:
              break;
            case esriFieldType.esriFieldTypeDouble:
              fieldValue = fieldValue + feature.getValue(i).toString() + " ";
              break;
            case esriFieldType.esriFieldTypeString:
              fieldValue = fieldValue + feature.getValue(i).toString() + " ";
              break;
            case esriFieldType.esriFieldTypeDate:
              fieldValue = fieldValue + feature.getValue(i).toString() + " ";
              break;
            case esriFieldType.esriFieldTypeOID:
              break;
            case esriFieldType.esriFieldTypeGeometry:
              fieldValue = fieldValue + "Shape" + " ";
              break;
          }
        }
        if (fieldValue != null) {
          System.out.println(fieldValue);
        }
        feature = fCursur.nextFeature();
      }
    }
    catch (Exception e) {
      System.out.println("Exception " + e.getMessage());
    }
  }

  public static void main(String[] args) {
    if (args.length < 2) {
      System.out.println(
          "QueryShapefile - ArcObjects Java SDK Developer Sample");
      System.out.println(
          "Usage: QueryShapefile <folderName featureClassName >");
      System.exit(0);
    }
    System.out.println(
        "QueryShapefile - ArcObjects Java SDK Developer Sample");
    EngineInitializer.initializeEngine();
    String shpPath = args[0];
    String shpName = args[1];
    QueryShapefile queryShapefile = new QueryShapefile();
    queryShapefile.viewRecordsInShapefile(shpPath, shpName);
    System.out.println("Done!");
    System.exit(0);
  }
}

⌨️ 快捷键说明

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