Nov 22

displaying shapefiles in wwjava

Tag: codedenis @ 3:48 am

here is a quick hack to add esri shapefiles support in worlwind java:

1- get gistoolkit
2- strip de bones (keep only gistoolkit.features.*, gistoolkit.common and gistoolkit.datasources.shapefile)

3 - Link to your project.

4- then you can add a shapefile to a layer by doing something like this, if you are really lazy:

ShapeFile shp = new ShapeFile(fn);
if (shp == null)
return;
try {
shp.readRecords();
} catch(Exception e) {
err("Error while reading shape.");
return;
}
ShapeFileRecord[] r = shp.getRecords();
int i, j;
fireLayer.clearList();
gistoolkit.features.Point lastCenter = null;
for(i=0; i<r.length; i++)
{
gistoolkit.features.Shape s = r[i].getShape();
if (s.getShapeType() == "MULTIPOLYGON") {
gistoolkit.features.MultiPolygon m = (gistoolkit.features.MultiPolygon) s;
gistoolkit.features.Polygon[] polys = m.getPolygons();
for(j=0; j<polys.length; j++) {
addPolygon(polys[j]);
lastCenter = polys[j].getCentroid();
}
}
}
 
private void addPolygon(gistoolkit.features.Polygon p)
{
int i;
ArrayList<LatLon> positions = new ArrayList<LatLon>();
gistoolkit.features.Point[] pts = p.getPoints();
for(i=0; i<pts.length; i++)
{
positions.add(LatLon.fromDegrees(pts[i].y, pts[i].x));
}
Dimension texSize = new Dimension();
texSize.setSize(1024, 1024);
SurfacePolygon fire = new SurfacePolygon(positions, new Color(1f, 0f, 0f), new Color(1f, 0f, 0f), texSize);
fire.setDrawInterior(true);
fireLayer.addRenderable(fire);
}

fireLayer is a RenderableLayer that was initialized previously.

texSize is needed so that the shape doesn’t look too blurry.

Bookmark this page on: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • Reddit
  • StumbleUpon
  • Technorati

3 Responses to “displaying shapefiles in wwjava”

  1. ayman says:

    That’s great, i Created polygon layer with only one feature (3 points) and it works, but if I add my data (polygon with 7 features and each polygon has 10-300 points) WWWJ hangs, also drag will not work if you click on the layer to move the map, but if you click on other area it works.

  2. denis says:

    Performances are not too bad over here with polygons with 3-200 points but you could either generate the shapefile in a thread or use the Douglas-Peucker polygon reduction algorithm.
    I have the same problem with the drag, let me know if you find any solutions.

  3. ayman says:

    fireLayer.setPickEnabled(false); will solve the drag problem

Leave a Reply