POST
|
I think I may have figured it out! After looking up the last error I found a link to this post: http://forums.esri.com/Thread.asp?c=159&f=1706&t=237300 And it turns out that my license never initialized. Once I fixed the license issue the program ran without any errors.
... View more
08-14-2012
01:05 PM
|
0
|
0
|
940
|
POST
|
Alright, made a little progress after taking a break - After having so much trouble opening the attribute table from the shapefile I decided to try to just read the *.dbf file directly. I created a new method and was able to open the dbf, list the heading and print out all the records. Then I decided to go back and try the ESRI way; I found the example: Open a DBase (dbf) file
In this case, pathToFile is the path to the folder holding the dbf file. Additionally, the file extension, .dbf, must be included when specifying the name of the DBase file.
[Java]
ShapefileWorkspaceFactory shapefileWorkspaceFactory = new ShapefileWorkspaceFactory();
Workspace workspace = new Workspace(shapefileWorkspaceFactory.openFromFile(pathToFile,0));
Table table = new Table(workspace.openTable(fileName)); so I created a new method that takes two strings one for the path to the folder containing the shapefile and the second as the filename to the dbf: public static void ArcDBF(String path, String fileName)
{
try{
ShapefileWorkspaceFactory shapefileWorkspaceFactory = new ShapefileWorkspaceFactory();
Workspace workspace = new Workspace(shapefileWorkspaceFactory.openFromFile(path,0));
Table table = new Table(workspace.openTable(fileName));
System.out.println("Success!");
}catch(AutomationException ae)
{
ae.printStackTrace();
}catch(IOException e)
{
e.printStackTrace();
}
} then I call the method from my main: mapDocument.open("D:\\Tellus_Sedimentary_Basin.mxd","");
IMap main_map = mapDocument.getMap(0);
IFeatureLayer layer = (IFeatureLayer) main_map.getLayer(0);
IDataLayer2 dataLayer = (IDataLayer2) layer;
IDatasetName name = (IDatasetName) dataLayer.getDataSourceName();
IWorkspaceName workspace = name.getWorkspaceName();
ArcDBF(workspace.getPathName()+"\\", "Tellus_Sedimentary_Basins_Layer.dbf"); but when I run the code I get these errors: at com.esri.arcgis.datasourcesfile.ShapefileWorkspaceFactory.openFromFile(Unknown Source)
at EngineTest.ArcDBF(EngineTest.java:113)
at EngineTest.main(EngineTest.java:69)
Unhandled Exception:
Unhandled Exception: System.IO.FileLoadException: Could not load file or assembly 'ESRI.DADF.Core.dll' or one of its dependencies. The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
File name: 'ESRI.DADF.Core.dll'
System.IO.FileLoadException: Could not load file or assembly 'ESRI.DADF.Core.dll' or one of its dependencies. The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
File name: 'ESRI.DADF.Core.dll' The command at EngineTest.java:113 is: Workspace workspace = new Workspace(shapefileWorkspaceFactory.openFromFile(path,0));
... View more
08-14-2012
11:40 AM
|
0
|
0
|
308
|
POST
|
IFeatureClass featureClass = fl.getFeatureClass(); This line is your problem, but stems from another issue. The featureClass variable in listAtributes is null, so when you try to call search on featureClass, you're getting the null pointer exception. Why is featureClass null? Seriously thanks for all your help but I was reading some documentation online and I think I'm missing a workspace. I'm going to leave this for now, relax this weekend, and comeback to this monday morning.
... View more
08-10-2012
01:11 PM
|
0
|
0
|
308
|
POST
|
I'm starting to think that it isn't so much the code but the logic that I'm having problems with; the way I'm thinking about it is: 1. initialize engine 2. open mdx 3. find which layers mdx has 4. open layer 5. open attribute table I know there is a problem at step 5 so am I missing something between step 4 and 5? Is there some intermediate step between opening a layer and opening the data within that layer? Is the data in a shapefile encapsulated in a personal geodatabase? Here is all the current code: public class EngineTest
{
@SuppressWarnings("deprecation")
public static void main(String[] args)
{
try
{
//Step 1: Initialize the Java Componet Object Model (COM) Interop.
EngineInitializer.initializeEngine();
//Step 2: Initialize a valid license.
new AoInitialize().initialize
(esriLicenseProductCode.esriLicenseProductCodeEngine);
//Step 3 : Invoke ArcObjects.
MapDocument mapDocument = new MapDocument();
if (mapDocument.isPresent("D:\\Tellus_Sedimentary_Basin.mxd"))
{
mapDocument.open("D:\\Tellus_Sedimentary_Basin.mxd","");
mapDocument.setAuthor("Brian Varela");
IMap main_map = mapDocument.getMap(0);
IFeatureLayer layer = (IFeatureLayer) main_map.getLayer(0);
IFeatureClass classLayer = layer.getFeatureClass();
IDataLayer2 dataLayer = (IDataLayer2) layer;
IDatasetName name = (IDatasetName) dataLayer.getDataSourceName();
IWorkspaceName workspace = name.getWorkspaceName();
//System.out.println("Number of Maps in this document: "+mapDocument.getMapCount());
System.out.println("SUCCESS "+mapDocument.getDocumentFilename()+" ArcMap file opened - Author: "+mapDocument.getAuthor());
ListLayers((Map)mapDocument.getMap(0));
System.out.println("Opened Layer "+layer.getName()+" located at "+workspace.getPathName()+" "+layer.getDisplayField());
FeatureLayer fl = (FeatureLayer) getLayerByName(main_map, layer.getName());
listAttributes(fl);
}
}
catch (IOException ex)
{
System.out.println(ex.getMessage());
System.out.println("App failed.");
}
finally
{
try
{
//Step 4: Release the license.
new AoInitialize().shutdown();
System.out.println("License released!");
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
} //End of method main.
public static void listAttributes(com.esri.arcgis.carto.FeatureLayer fl)
{
try {
IFeatureClass featureClass = fl.getFeatureClass();
IFeatureCursor updateCursor = featureClass.search(null, false);
int theFieldIndex = featureClass.findField("BASIN_NAME");
IFeature feature = null;
while((feature = updateCursor.nextFeature()) != null)
{
System.out.println( (String)feature.getValue(theFieldIndex));
}
} catch (AutomationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void ListLayers(com.esri.arcgis.carto.Map map)
{
try{
for(int i=0; i<map.getLayerCount(); i++)
{
System.out.println("Layer "+i+". "+map.getLayer(i).getName());
}
}catch(AutomationException ae)
{
ae.printStackTrace();
}catch(IOException e)
{
e.printStackTrace();
}
}
/**
* ESRI sample method to get feature class layer by name, instead of index.
* @param layerName the layer name as <code>String</code>.
* @return layer as <code>FeatureLayer</code>.
*/
public static FeatureLayer getLayerByName(IMap s_iMap, String layerName) {
FeatureLayer layer = null;
try {
for (int i = 0; i < s_iMap.getLayerCount(); i++) {
if (s_iMap.getLayer(i).getName().equalsIgnoreCase(layerName)) {
layer = (FeatureLayer) s_iMap.getLayer(i);
break;
}
}
} catch (AutomationException ae) {
ae.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// Returns the featurelayer
return layer;
}
}
... View more
08-10-2012
07:16 AM
|
0
|
0
|
308
|
POST
|
Well, keep at it. Sooner or later you will debug your code and realize that the method is not the problem. In your code, what is to the left of this line? IFeatureLayer or FeatureLayer? listAttributes((FeatureLayer) main_map.getLayer(0)) There is nothing to the left in my code; the listAttributes method I created doesn't return anything and only takes a FeatureLayer as an import: public static void listAttributes(com.esri.arcgis.carto.FeatureLayer fl)
{
try {
IFeatureClass featureClass = fl.getFeatureClass();
IFeatureCursor updateCursor = featureClass.search(null, false);
int theFieldIndex = featureClass.findField("BASIN_NAME");
IFeature feature = null;
while((feature = updateCursor.nextFeature()) != null)
{
System.out.println( (String)feature.getValue(theFieldIndex));
}
} catch (AutomationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} Thanks again for all your help - I know you are right "keep at it. Sooner or later you will debug your code and realize that the method is not the problem." I'm just so new to the arcobjects API and ESRI structure that I know it will take some time; I'm trying to get the java sdk so I can look at some samples but none of the people who have access to the EDN at my work can find the download.
... View more
08-09-2012
01:04 PM
|
0
|
0
|
308
|
POST
|
FeatureLayer fl = (FeatureLayer) getLayerByName("your layer name as it is spelled in the map document");
/**
* ESRI sample method to get feature class layer by name, instead of index.
* @param layerName the layer name as <code>String</code>.
* @return layer as <code>FeatureLayer</code>.
*/
public FeatureLayer getLayerByName(String layerName) {
FeatureLayer layer = null;
try {
for (int i = 0; i < s_iMap.getLayerCount(); i++) {
if (s_iMap.getLayer(i).getName().equalsIgnoreCase(layerName)) {
layer = (FeatureLayer) s_iMap.getLayer(i);
break;
}
}
} catch (AutomationException ae) {
ae.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// Returns the featurelayer
return layer;
}
s_iMap is a reference to the current IMap of your map document. Thanks again but I tried to use that method and I still get the NullPointer =( I then looked at the code and it looks like ESRI's method was doing the same thing I tried before except they loop through the layers looking for the name that matches: ESRI's code that creates the Feature layer: layer = (FeatureLayer) s_iMap.getLayer(i) looks like what I was trying: listAttributes((FeatureLayer) main_map.getLayer(0))
... View more
08-09-2012
11:16 AM
|
0
|
0
|
632
|
POST
|
That is probably because your fl is an IFeatureLayer, when you need a FeatureLayer. How can I get the FeatureLayer from an mxd? I tried casting it from the IMap: listAttributes((FeatureLayer) main_map.getLayer(0)); but that didn't work; I've been trying to use the API but there is just a lot there.
... View more
08-09-2012
08:26 AM
|
0
|
0
|
632
|
POST
|
Bummer. On what line? I'm still getting the NullPointer on IFeatureCursor updateCursor = featureClass.search(null, false); the documentation says: "If a null value is passed to the filter parameter, the feature cursor will return all of the features from the feature class."
... View more
08-09-2012
06:50 AM
|
0
|
0
|
632
|
POST
|
Thank you for your help but I'm still getting a java.lang.NullPointerException. I didn't need the query part of your method so I reworked it a little bit: My main method: .
.
.
IMap main_map = mapDocument.getMap(0);
IFeatureLayer layer = (IFeatureLayer) main_map.getLayer(0);
listAttributes((FeatureLayer) layer); The list attributes method: public static void listAttributes(com.esri.arcgis.carto.FeatureLayer fl)
{
try {
IFeatureClass featureClass = fl.getFeatureClass();
IFeatureCursor updateCursor = featureClass.search(null, false);
int theFieldIndex = featureClass.findField("BASIN_NAME");
IFeature feature = null;
while((feature = updateCursor.nextFeature()) != null)
{
System.out.println( (String)feature.getValue(theFieldIndex));
}
} catch (AutomationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
... View more
08-09-2012
05:58 AM
|
0
|
0
|
632
|
POST
|
I am new to arcobjects and GIS in general so excuse my noobness. I'm trying to create a console based application that eventually will raster some polygons then do some stats on the rasters but right now I just want to be able to list all the features?(a single attribute from a layer's attribute table) from a given layer/shapefile in an existing mxd. Right now, I can initialize the arc engine, open an mxd, list all the layers within that mxd and open a shapefile from a layer within the mxd. What I need help with is being able to loop through the attribute table for the shapefile I opened from the mxd. Here is the code I have so far: public class EngineTest { @SuppressWarnings("deprecation") public static void main(String[] args) { try { //Step 1: Initialize the Java Componet Object Model (COM) Interop. EngineInitializer.initializeEngine(); //Step 2: Initialize a valid license. new AoInitialize().initialize (esriLicenseProductCode.esriLicenseProductCodeEngine); //Step 3 : Invoke ArcObjects. MapDocument mapDocument = new MapDocument(); if (mapDocument.isPresent("D:\\Tellus_Sedimentary_Basin.mxd")) { mapDocument.open("D:\\Tellus_Sedimentary_Basin.mxd",""); mapDocument.setAuthor("Brian Varela"); IMap main_map = mapDocument.getMap(0); IFeatureLayer layer = (IFeatureLayer) main_map.getLayer(0); IFeatureClass classLayer = layer.getFeatureClass(); IDataLayer2 dataLayer = (IDataLayer2) layer; IDatasetName name = (IDatasetName) dataLayer.getDataSourceName(); IWorkspaceName workspace = name.getWorkspaceName(); ListLayers((Map)mapDocument.getMap(0)); System.out.println("Layer "+layer.getName()+" located at "+workspace.getPathName()+" "+layer.getDisplayField()); } } catch (IOException ex) { System.out.println(ex.getMessage()); System.out.println("App failed."); } finally { try { //Step 4: Release the license. new AoInitialize().shutdown(); System.out.println("License released!"); } catch (IOException ex) { ex.printStackTrace(); } } } //End of method main. public static void ListLayers(com.esri.arcgis.carto.Map map) { try{ for(int i=0; i<map.getLayerCount(); i++) { System.out.println("Layer "+i+". "+map.getLayer(i).getName()); } }catch(AutomationException ae) { ae.printStackTrace(); }catch(IOException e) { e.printStackTrace(); } } } //End of class
... View more
08-08-2012
12:58 PM
|
0
|
16
|
1630
|
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:23 AM
|