ERROR
When a polygon feature with three vertices is added to a shapefile, the following error message may occur:"java.lang.IllegalArgumentException"However, the same code works if the feature to be added has four or more vertices.
The polygon boundary is not closed. The boundary is closed automatically if the number of vertices is 4 or more.
Code:
//get the vertices
com.esri.mo.cs.geom.Point point1=getFirstPoint();
com.esri.mo.cs.geom.Point point2=getSecondPoint();
com.esri.mo.cs.geom.Point point3=getThirdPoint();
//number of vertices
int num = 3;
//read shapefile and get shape field
ShpFile shpfile=new ShpFile("D:/temp/testpoly.shp");
PolygonShp polyshp= (PolygonShp)shpfile.getField();
polyshp.setNumParts(1);
//set number of points as number+1
polyshp.setNumPoints(0,num+1);
polyshp.setXAt(0,0,point1.x);
polyshp.setYAt(0,0,point1.y);
polyshp.setXAt(0,1,point2.x);
polyshp.setYAt(0,1,point2.y);
polyshp.setXAt(0,2,point3.x);
polyshp.setYAt(0,2,point3.y);
//add first point at index=number of vertices
polyshp.setXAt(0,3,point1.x);
polyshp.setYAt(0,3,point1.y);
Note:
The example code uses classes from MapObjects Java 1.0.1. For MapObjects Java 2.0, use corresponding classes from this version.
Article ID:000005610
Get help from ArcGIS experts
Download the Esri Support App