RE: [OpenMap Users] AreaShapeLayer question

From: Chris Hopkins <chopkins_at_cra.com>
Date: Wed, 24 Jan 2007 06:05:28 -0500

Thanks a lot Don! In case anyone was curious, this is the code I wrote
that performs the aggregation of areas. I wrote it using the
java.awt.geom package classes and, for now, have stuck it into my own
extension of the BasicMapPanel. I also realized that the OMPoly object's
have the Integer index set as their appObject and you can look up the
name of the OMPoly based on that Integer so that worked out well. In any
case, it's working the way we expect now.

  - Chris

------------------------------------------
package com.cra.openmap.gui;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.geom.Area;
import java.awt.geom.GeneralPath;
import java.awt.geom.PathIterator;
import java.util.ArrayList;

import com.bbn.openmap.gui.BasicMapPanel;
import com.bbn.openmap.layer.shape.areas.AreaHandler;
import com.bbn.openmap.omGraphics.OMGraphic;
import com.bbn.openmap.omGraphics.OMGraphicList;
import com.bbn.openmap.omGraphics.OMPoly;
import com.cra.openmap.layer.MyAreaShapeLayer;

public class OpenMapPanel extends BasicMapPanel
{
  private AreaHandler areaHandler;

  private MyAreaShapeLayer areaShapeLayer;

  private final BasicStroke REGION_STROKE = new BasicStroke(2.0f);
  
  public OpenMapPanel()
  {
    super(null);
  }

  _at_Override
  public void findAndInit(Object arg0)
  {
    super.findAndInit(arg0);

    if (arg0 instanceof MyAreaShapeLayer)
    {
      System.err.println("Got the area handler!!!");
      this.areaShapeLayer = (MyAreaShapeLayer)arg0;
      this.areaHandler = (AreaHandler)this.areaShapeLayer.getAreas();

      this.areaShapeLayer.addRegion(createRegion(Color.blue.brighter(),
"Al Anbar", "Karbala'",
          "Baghdad", "Babil", "An Najaf"));
      this.areaShapeLayer.addRegion(createRegion(Color.green.brighter(),
"Ninawa", "Dahuk",
          "Arbil", "As Sulaymaniyah", "At Ta'min", "Salah ad Din",
"Diyala"));
      this.areaShapeLayer.addRegion(createRegion(Color.red.brighter(),
"Wasit", "Maysan",
          "Al Qadisiyah", "Dhi Qar", "Al Basrah", "Al Muthanna"));

      this.areaShapeLayer.repaint();
    }
  }

  private OMPoly createRegion(Color polyColor, String... strings)
  {
    OMPoly regionPoly = null;

    if (strings != null && strings.length > 0)
    {
      // Create an AWT Area object from the first political area
      Area area = createAwtAreaFromPoliticalArea(strings[0]);

      if (area != null)
      {
        for (int i = 1; i < strings.length; i++)
        {
          Area nextArea = createAwtAreaFromPoliticalArea(strings[i]);
          if (nextArea != null)
          {
            area.add(nextArea);
          }
        }
      }

      regionPoly = createOMPolyFromArea(area, polyColor);
    }

    return regionPoly;
  }

  private OMPoly createOMPolyFromArea(Area area, Color polyColor)
  {
    ArrayList<Float> newpoints = new ArrayList<Float>();
    PathIterator pi = area.getPathIterator(null);
    float[] fpts = new float[6];
    while (!pi.isDone())
    {
      int type = pi.currentSegment(fpts);
      if (type == PathIterator.SEG_MOVETO || type ==
PathIterator.SEG_LINETO)
      {
        newpoints.add(fpts[0]);
        newpoints.add(fpts[1]);
      }

      pi.next();
    }

    float[] newfpts = new float[newpoints.size()];
    for (int i = 0; i < newpoints.size(); i++)
    {
      newfpts[i] = newpoints.get(i);
    }

    OMPoly newpoly = new OMPoly(newfpts, OMGraphic.RADIANS,
OMGraphic.LINETYPE_STRAIGHT);
    newpoly.setLinePaint(polyColor);
    newpoly.setStroke(REGION_STROKE);

    return newpoly;
  }

  private Area createAwtAreaFromPoliticalArea(String string)
  {
    Area newArea = null;
    OMPoly omp = getOMPolyForArea(string);
    if (omp != null)
    {
      newArea = new Area(createGeneralPath(omp.getLatLonArray()));
    }

    return newArea;
  }

  private GeneralPath createGeneralPath(float[] pts)
  {
    GeneralPath gp = new GeneralPath();
    gp.moveTo(pts[0], pts[1]);

    for (int i = 2; i < pts.length; i += 2)
    {
      gp.lineTo(pts[i], pts[i + 1]);
    }

    gp.closePath();

    return gp;
  }

  private OMPoly getOMPolyForArea(String string)
  {
    OMGraphicList omgl = this.areaHandler.getGraphics();
    for (int i = 0; i < omgl.size(); i++)
    {
      OMGraphic omg = omgl.getOMGraphicAt(i);
      if (omg instanceof OMGraphicList)
      {
        OMGraphicList omgl2 = (OMGraphicList)omg;
        for (int j = 0; j < omgl2.size(); j++)
        {
          OMGraphic omg2 = omgl2.getOMGraphicAt(j);
          if (omg2 instanceof OMPoly)
          {
            String name =
this.areaHandler.getName((Integer)omg2.getAppObject());
            if (name != null && name.equals(string))
            {
              return (OMPoly)omg2;
            }
          }
        }
      }
    }

    return null;
  }
}

--
[To unsubscribe to this list send an email to "majdart_at_bbn.com"
with the following text in the BODY of the message "unsubscribe openmap-users"]
Received on Wed Jan 24 2007 - 06:07:50 EST

This archive was generated by hypermail 2.3.0 : Tue Mar 28 2017 - 23:25:08 EDT