English

Error: java.lang.IllegalArgumentException error with a new triangular feature

Error Message

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.

Cause

The polygon boundary is not closed. The boundary is closed automatically if the number of vertices is 4 or more.

Solution or Workaround

Close a polygon boundary with these steps:

  1. Set the number of points to number of vertices + 1.
  2. Get x, y coordinates for each vertex and set them to the polygon at respective indices.
  3. Set the first point again at index = "number of vertices".
    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.