⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gdfreader.java

📁 linux下用于移动节点的移动活动生成工具
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
          else
            point = (GDFXYZRecord.XYZPoint)edge.tNode.point.points.get(0);

          points.add(convertToLocalCoordinates(point));
        }

        // add intermediate points
        if (edge.points!=null)
        {
          java.util.ArrayList coordinates = edge.points.points;
          int m = (direction==0)? 0 : coordinates.size()-1;

          while ((m>=0)&&(m<coordinates.size()))
          {
            point = (GDFXYZRecord.XYZPoint)coordinates.get(m);
            points.add(convertToLocalCoordinates(point));

            if (direction==0)
              m++;
            else
              m--;
          }
        }

        // add ending point of an edge
        if (direction==0)
          point = (GDFXYZRecord.XYZPoint)edge.tNode.point.points.get(0);
        else
          point = (GDFXYZRecord.XYZPoint)edge.fNode.point.points.get(0);

        points.add(convertToLocalCoordinates(point));
      }

      // add junctions if no edges are specified
      if (points.size()==0)
      {
        // add starting Junction
        point = (GDFXYZRecord.XYZPoint)((GDFNodeRecord)
          feature.fPoint.nodes.get(0)).point.points.get(0);
        points.add(convertToLocalCoordinates(point));

        // add ending Junction
        point = (GDFXYZRecord.XYZPoint)((GDFNodeRecord)
          feature.tPoint.nodes.get(0)).point.points.get(0);
        points.add(convertToLocalCoordinates(point));
      }

      SpatialModelElement element = new SpatialModelElement(id, class_code, subclass_code, shape);

      // copy default attributes
      element.getAttributes().putAll(defaultAttributesValues);

      // copy attributes
      for (int i=0; i<feature.attributes.size(); i++)
      {
        GDFAttributeRecord attr = (GDFAttributeRecord)feature.attributes.get(i);
        if (attr==null)
          continue;

        element.getAttributes().putAll(attr.attributes);
      }

      // clip
      if (clipArea!=null)
      {
        clip(element);
        if (shape.getPoints().size()>0)
          elements.put(id, element);
      }
      else
        elements.put(id, element);
    }
  }

  /**
   * Constructs elements from loaded area features
   */
  protected void createAreaFeatureElements()
  {
    java.util.Map elements = spatialModel.getElements();

    Object sections[] = this.sections.values().toArray();
    GDFSection section = (GDFSection)sections[0];

    // process area features
    java.util.Iterator iter = section.areaFeatures.keySet().iterator();
outer:
    while (iter.hasNext())
    {
      String id = (String)iter.next();
      GDFAreaFeatureRecord feature = (GDFAreaFeatureRecord)
        section.areaFeatures.get(id);
      String class_code = feature.featureClassCode.substring(0, 2);
      String subclass_code = feature.featureClassCode.substring(2, 4);

      Polygon shape = new Polygon();
      java.util.ArrayList points = shape.getPoints();

      // check if the feature is correct
      for (int i=0; i<feature.faces.size(); i++)
      {
        GDFFaceRecord face = (GDFFaceRecord)feature.faces.get(i);
        if (face==null)
          continue outer;

        for (int l=0; l<face.edges.size(); l++)
        {
          GDFEdgeRecord edge = (GDFEdgeRecord)face.edges.get(l);
          if (edge==null)
            continue outer;
        }
      }

      for (int i=0; i<feature.faces.size(); i++)
      {
        GDFFaceRecord face = (GDFFaceRecord)feature.faces.get(i);

        GDFXYZRecord.XYZPoint point;

        // add intermediate edges
        for (int l=0; l<face.edges.size(); l++)
        {
          GDFEdgeRecord edge = (GDFEdgeRecord)face.edges.get(l);

          int direction = ((Integer)face.edgesDirections.get(l)).intValue();

          if (l==0)
          {
            // add starting point of an edge
            if (direction==0)
              point = (GDFXYZRecord.XYZPoint)edge.fNode.point.points.get(0);
            else
              point = (GDFXYZRecord.XYZPoint)edge.tNode.point.points.get(0);

            points.add(convertToLocalCoordinates(point));
          }

          // add intermediate points
          if (edge.points!=null)
          {
            java.util.ArrayList coordinates = edge.points.points;
            int m = (direction==0)? 0 : coordinates.size()-1;

            while ((m>=0)&&(m<coordinates.size()))
            {
              point = (GDFXYZRecord.XYZPoint)coordinates.get(m);
              points.add(convertToLocalCoordinates(point));

              if (direction==0)
                m++;
              else
                m--;
            }
          }

          // add ending point of an edge
          if (direction==0)
            point = (GDFXYZRecord.XYZPoint)edge.tNode.point.points.get(0);
          else
            point = (GDFXYZRecord.XYZPoint)edge.fNode.point.points.get(0);

          points.add(convertToLocalCoordinates(point));
        }
      }

      SpatialModelElement element = new SpatialModelElement(id, class_code, subclass_code, shape);

      // copy default attributes
      element.getAttributes().putAll(defaultAttributesValues);

      // copy attributes
      for (int i=0; i<feature.attributes.size(); i++)
      {
        GDFAttributeRecord attr = (GDFAttributeRecord)feature.attributes.get(i);
        if (attr==null)
          continue;

        element.getAttributes().putAll(attr.attributes);
      }

      // clip
      if (clipArea!=null)
      {
        clip(element);
        if (shape.getPoints().size()>0)
          elements.put(id, element);
      }
      else
        elements.put(id, element);
    }
  }

  /**
   * Constructs elements from loaded complex features
   */
  protected void createComplexFeatureElements()
  {
    java.util.Map elements = spatialModel.getElements();

    Object sections[] = this.sections.values().toArray();
    GDFSection section = (GDFSection)sections[0];

    // process complex features
    java.util.Iterator iter = section.complexFeatures.keySet().iterator();
    while (iter.hasNext())
    {
      String id = (String)iter.next();
      GDFComplexFeatureRecord feature = (GDFComplexFeatureRecord)
        section.complexFeatures.get(id);
      String class_code = feature.featureClassCode.substring(0, 2);
      String subclass_code = feature.featureClassCode.substring(2, 4);

      SpatialModelElement element = new SpatialModelElement(id, class_code, subclass_code, null);

      // add child elements
      for (int i=0; i<feature.pointFeaturesID.size(); i++)
      {
        SpatialModelElement e = spatialModel.getElement((String)feature.pointFeaturesID.get(i));
        if (e!=null)
          element.getChildren().add(e);
      }

      for (int i=0; i<feature.lineFeaturesID.size(); i++)
      {
        SpatialModelElement e = spatialModel.getElement((String)feature.lineFeaturesID.get(i));
        if (e!=null)
          element.getChildren().add(e);
      }

      for (int i=0; i<feature.areaFeaturesID.size(); i++)
      {
        SpatialModelElement e = spatialModel.getElement((String)feature.areaFeaturesID.get(i));
        if (e!=null)
          element.getChildren().add(e);
      }

      for (int i=0; i<feature.complexFeaturesID.size(); i++)
      {
        SpatialModelElement e = spatialModel.getElement((String)feature.complexFeaturesID.get(i));
        if (e!=null)
          element.getChildren().add(e);
      }

      // copy default attributes
      element.getAttributes().putAll(defaultAttributesValues);

      // copy attributes
      for (int i=0; i<feature.attributes.size(); i++)
      {
        GDFAttributeRecord attr = (GDFAttributeRecord)feature.attributes.get(i);
        if (attr==null)
          continue;

        element.getAttributes().putAll(attr.attributes);
      }

      elements.put(id, element);
    }
  }

  /**
   * Constructs elements from loaded GDF data
   */
  protected void createElements()
  {
    createPointFeatureElements();
    createLineFeatureElements();
    createAreaFeatureElements();
    createComplexFeatureElements();
  }

  /**
   * Converts the Point to the local cartesian coordinate system. <br>
   * <br>
   * @param point point
   * @return point in the local cartesian coordinate system
   */
  protected Point convertToLocalCoordinates(GDFXYZRecord.XYZPoint point)
  {
    Utm_Coord_3d utm = new Utm_Coord_3d();
    Gdc_To_Utm_Converter.Convert(new Gdc_Coord_3d(point.y*scale_y, point.x*scale_x, 0), utm);

    Point res = new Point(utm.x-origin.x, utm.y-origin.y);

    return res;
  }

  /**
   * Clips the Element. <br>
   * <br>
   * @param elem element to clip
   */
  protected void clip(SpatialModelElement elem)
  {
    java.util.ArrayList points = ((Polyline)elem.getGeometry()).getPoints();
    java.util.ArrayList resPoints = new java.util.ArrayList();

    // iterate all points
    boolean flag = true;
    for (int i=0; i<points.size(); i++)
    {
      Point point = (Point)points.get(i);
      if (clipArea.contains(point))
      {
        if (flag)
          resPoints.add(point);
        else
        {
          // do clipping
          Line tempLine = new Line((Point)points.get(i-1), point);

          // add clipping point
          for (int j=0; j<clipArea.getPoints().size(); j++)
          {
            Line boundLine = new Line((Point)clipArea.getPoints().get(j),
              (Point)clipArea.getPoints().get((j+1)%clipArea.getPoints().size()));
            Point p = tempLine.intersect(boundLine);
            if (p!=null)
            {
              resPoints.add(p);
              break;
            }
          }

          // add point
          resPoints.add(point);
        }

        flag = true;
      }
      else
      {
        if ((flag)&&(i>0))
        {
          // do clipping
          Line tempLine = new Line((Point)points.get(i-1), point);

          // add clipping point
          for (int j=0; j<clipArea.getPoints().size(); j++)
          {
            Line boundLine = new Line((Point)clipArea.getPoints().get(j),
              (Point)clipArea.getPoints().get((j+1)%clipArea.getPoints().size()));
            Point p = tempLine.intersect(boundLine);
            if (p!=null)
            {
              resPoints.add(p);
              break;
            }
          }
        }

        flag = false;
      }
    }

    points.clear();
    points.addAll(resPoints);
  }

  /**
    * Initializes the object from XML tag. <br>
    * <br>
    * @param element source tag
    * @throws Exception Exception if parameters are invalid
    */
  public void load(org.w3c.dom.Element element) throws Exception
  {
    u.sendNotification(new LoaderNotification(this, u,
      "Loading GDFReader extension"));

    super.load(element);

    spatialModel = (SpatialModel)u.getExtension("SpatialModel");

    String s;

    s = element.getAttribute("scale_x");
    if(s.length()>0)
      scale_x = Float.parseFloat(s);

    s = element.getAttribute("scale_y");
    if(s.length()>0)
      scale_y = Float.parseFloat(s);

    float min_x_clip = Float.NaN, max_x_clip = Float.NaN,
          min_y_clip = Float.NaN, max_y_clip = Float.NaN;

    s = element.getAttribute("min_x");
    if(s.length()>0)
      min_x_clip = Float.parseFloat(s);

    s = element.getAttribute("max_x");
    if(s.length()>0)
      max_x_clip = Float.parseFloat(s);

    s = element.getAttribute("min_y");
    if(s.length()>0)
      min_y_clip = Float.parseFloat(s);

    s = element.getAttribute("max_y");
    if(s.length()>0)
      max_y_clip = Float.parseFloat(s);

    if ( Float.isNaN(min_x_clip)&&Float.isNaN(max_x_clip)
       &&Float.isNaN(min_y_clip)&&Float.isNaN(max_y_clip) )
    {
    }
    else
    {
      if ( (Float.isNaN(min_x_clip)||Float.isNaN(max_x_clip)
            ||Float.isNaN(min_y_clip)||Float.isNaN(max_y_clip))
         ||((min_x_clip>=max_x_clip)||(min_y_clip>=max_y_clip)) )
        throw new Exception("Invalid clip region");

      clipArea = new Polygon();
      clipArea.getPoints().add(new Point(min_x_clip, min_y_clip));
      clipArea.getPoints().add(new Point(max_x_clip, min_y_clip));
      clipArea.getPoints().add(new Point(max_x_clip, max_y_clip));
      clipArea.getPoints().add(new Point(min_x_clip, max_y_clip));
    }

    s = element.getAttribute("source");
    if(s.length()==0)
      throw new Exception("Invalid source name: "+s);

    source = new BufferedReader(new InputStreamReader(new FileInputStream(s), "ISO-8859-1"));

    // prepare the converter, use wgs'84 by default
    Gdc_To_Utm_Converter.Init();

    doLoad();
    resolveLinks();

    createElements();

    spatialModel.rebuildGraph();

    u.sendNotification(new LoaderNotification(this, u,
      "Finished loading GDFReader extension"));
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -