Exemplo n.º 1
0
void ILI1Reader::ReadGeom( char **stgeom, int geomIdx, OGRwkbGeometryType eType,
                           OGRFeature *feature ) {
#ifdef DEBUG_VERBOSE
    CPLDebug( "OGR_ILI",
              "ILI1Reader::ReadGeom geomIdx: %d OGRGeometryType: %s",
              geomIdx, OGRGeometryTypeToName(eType) );
#endif
    if (eType == wkbNone)
    {
      CPLError( CE_Warning, CPLE_AppDefined,
                "Calling ILI1Reader::ReadGeom with wkbNone" );
    }

    // Initialize geometry.

    OGRCompoundCurve *ogrCurve = new OGRCompoundCurve();
    OGRCurvePolygon *ogrPoly = NULL; //current polygon
    OGRMultiCurve *ogrMultiLine = NULL; //current multi line

    if (eType == wkbMultiCurve || eType == wkbMultiLineString)
    {
      ogrMultiLine = new OGRMultiCurve();
    }
    else if (eType == wkbPolygon || eType == wkbCurvePolygon)
    {
      ogrPoly = new OGRCurvePolygon();
    }

    OGRPoint ogrPoint; // Current point.
    ogrPoint.setX(CPLAtof(stgeom[1])); ogrPoint.setY(CPLAtof(stgeom[2]));

    OGRLineString *ogrLine = new OGRLineString();
    ogrLine->addPoint(&ogrPoint);

    // Parse geometry.

    char **tokens = NULL;
    bool end = false;
    OGRCircularString *arc = NULL; //current arc

    while (!end && (tokens = ReadParseLine()) != NULL)
    {
      const char *firsttok = CSLGetField(tokens, 0);
      if( firsttok == NULL )
      {
          // do nothing
      }
      else if (EQUAL(firsttok, "LIPT") && CSLCount(tokens) >= 3)
      {
        ogrPoint.setX(CPLAtof(tokens[1])); ogrPoint.setY(CPLAtof(tokens[2]));
        if (arc) {
          arc->addPoint(&ogrPoint);
          OGRErr error =  ogrCurve->addCurveDirectly(arc);
          if (error != OGRERR_NONE) {
            char* pszJSon = arc->exportToJson();
            CPLError(CE_Warning, CPLE_AppDefined, "Could not add geometry: %s",
                     pszJSon ? pszJSon : "(null)" );
            CPLFree(pszJSon);
            delete arc;
          }
          arc = NULL;
        }
        ogrLine->addPoint(&ogrPoint);
      }
      else if (EQUAL(firsttok, "ARCP") && CSLCount(tokens) >= 3)
      {
        //Finish line and start arc
        if (ogrLine->getNumPoints() > 1) {
          OGRErr error = ogrCurve->addCurveDirectly(ogrLine);
          if (error != OGRERR_NONE) {
            char* pszJSon = ogrLine->exportToJson();
            CPLError(CE_Warning, CPLE_AppDefined, "Could not add geometry: %s",
                     pszJSon ? pszJSon : "(null)" );
            CPLFree(pszJSon);
            delete ogrLine;
          }
          ogrLine = new OGRLineString();
        } else {
          ogrLine->empty();
        }
        delete arc;
        arc = new OGRCircularString();
        arc->addPoint(&ogrPoint);
        ogrPoint.setX(CPLAtof(tokens[1])); ogrPoint.setY(CPLAtof(tokens[2]));
        arc->addPoint(&ogrPoint);
      }
      else if (EQUAL(firsttok, "ELIN"))
      {
        if (ogrLine->getNumPoints() > 1) { // Ignore single LIPT after ARCP
          OGRErr error = ogrCurve->addCurveDirectly(ogrLine);
          if (error != OGRERR_NONE) {
            char* pszJSon = ogrLine->exportToJson();
            CPLError(CE_Warning, CPLE_AppDefined, "Could not add geometry: %s",
                     pszJSon ? pszJSon : "(null)" );
            CPLFree(pszJSon);
            delete ogrLine;
          }
          ogrLine = NULL;
        }
        if (!ogrCurve->IsEmpty()) {
          if (ogrMultiLine)
          {
            OGRErr error = ogrMultiLine->addGeometryDirectly(ogrCurve);
            if (error != OGRERR_NONE) {
              char* pszJSon = ogrCurve->exportToJson();
              CPLError(CE_Warning, CPLE_AppDefined, "Could not add geometry: %s",
                       pszJSon ? pszJSon : "(null)" );
              CPLFree(pszJSon);
              delete ogrCurve;
            }
            ogrCurve = NULL;
          }
          if (ogrPoly)
          {
            OGRErr error = ogrPoly->addRingDirectly(ogrCurve);
            if (error != OGRERR_NONE) {
              char* pszJSon = ogrCurve->exportToJson();
              CPLError(CE_Warning, CPLE_AppDefined, "Could not add geometry: %s",
                       pszJSon ? pszJSon : "(null)" );
              CPLFree(pszJSon);
              delete ogrCurve;
            }
            ogrCurve = NULL;
          }
        }
        end = true;
      }
      else if (EQUAL(firsttok, "EEDG"))
      {
        end = true;
      }
      else if (EQUAL(firsttok, "LATT"))
      {
        //Line Attributes (ignored)
      }
      else if (EQUAL(firsttok, "EFLA"))
      {
        end = true;
      }
      else if (EQUAL(firsttok, "ETAB"))
      {
        end = true;
      }
      else
      {
        CPLError( CE_Warning, CPLE_AppDefined,
                  "Unexpected token: %s", firsttok );
      }

      CSLDestroy(tokens);
    }
    delete arc;

    delete ogrLine;

    //Set feature geometry
    if (eType == wkbMultiCurve)
    {
      feature->SetGeomFieldDirectly(geomIdx, ogrMultiLine);
      delete ogrCurve;
    }
    else if (eType == wkbMultiLineString)
    {
      feature->SetGeomFieldDirectly(geomIdx, ogrMultiLine->getLinearGeometry());
      delete ogrMultiLine;
      delete ogrCurve;
    }
    else if (eType == wkbCurvePolygon)
    {
      feature->SetGeomFieldDirectly(geomIdx, ogrPoly);
      delete ogrCurve;
    }
    else if (eType == wkbPolygon)
    {
      feature->SetGeomFieldDirectly(geomIdx, ogrPoly->getLinearGeometry());
      delete ogrPoly;
      delete ogrCurve;
    }
    else
    {
      feature->SetGeomFieldDirectly(geomIdx, ogrCurve);
    }
}
Exemplo n.º 2
0
void ILI1Reader::ReadGeom(char **stgeom, int geomIdx, OGRwkbGeometryType eType, OGRFeature *feature) {
    char **tokens = NULL;
    const char *firsttok = NULL;
    int end = FALSE;
    OGRCompoundCurve *ogrCurve = NULL; //current compound curve
    OGRLineString *ogrLine = NULL; //current line
    OGRCircularString *arc = NULL; //current arc
    OGRCurvePolygon *ogrPoly = NULL; //current polygon
    OGRPoint ogrPoint; //current point
    OGRMultiCurve *ogrMultiLine = NULL; //current multi line

    //CPLDebug( "OGR_ILI", "ILI1Reader::ReadGeom geomIdx: %d OGRGeometryType: %s", geomIdx, OGRGeometryTypeToName(eType));
    if (eType == wkbNone)
    {
      CPLError(CE_Warning, CPLE_AppDefined, "Calling ILI1Reader::ReadGeom with wkbNone" );
    }

    //Initialize geometry

    ogrCurve = new OGRCompoundCurve();

    if (eType == wkbMultiCurve || eType == wkbMultiLineString)
    {
      ogrMultiLine = new OGRMultiCurve();
    }
    else if (eType == wkbPolygon || eType == wkbCurvePolygon)
    {
      ogrPoly = new OGRCurvePolygon();
    }

    //tokens = ["STPT", "1111", "22222"]
    ogrPoint.setX(CPLAtof(stgeom[1])); ogrPoint.setY(CPLAtof(stgeom[2]));
    ogrLine = new OGRLineString();
    ogrLine->addPoint(&ogrPoint);

    //Parse geometry
    while (!end && (tokens = ReadParseLine()))
    {
      firsttok = CSLGetField(tokens, 0);
      if (EQUAL(firsttok, "LIPT"))
      {
        ogrPoint.setX(CPLAtof(tokens[1])); ogrPoint.setY(CPLAtof(tokens[2]));
        if (arc) {
          arc->addPoint(&ogrPoint);
          ogrCurve->addCurveDirectly(arc);
          arc = NULL;
        }
        ogrLine->addPoint(&ogrPoint);
      }
      else if (EQUAL(firsttok, "ARCP"))
      {
        //Finish line and start arc
        if (ogrLine->getNumPoints() > 1) {
          ogrCurve->addCurveDirectly(ogrLine);
          ogrLine = new OGRLineString();
        } else {
          ogrLine->empty();
        }
        arc = new OGRCircularString();
        arc->addPoint(&ogrPoint);
        ogrPoint.setX(CPLAtof(tokens[1])); ogrPoint.setY(CPLAtof(tokens[2]));
        arc->addPoint(&ogrPoint);
      }
      else if (EQUAL(firsttok, "ELIN"))
      {
        if (!ogrLine->IsEmpty()) {
          ogrCurve->addCurveDirectly(ogrLine);
        }
        if (!ogrCurve->IsEmpty()) {
          if (ogrMultiLine)
          {
            ogrMultiLine->addGeometryDirectly(ogrCurve);
          }
          if (ogrPoly)
          {
            ogrPoly->addRingDirectly(ogrCurve);
          }
        }
        end = TRUE;
      }
      else if (EQUAL(firsttok, "EEDG"))
      {
        end = TRUE;
      }
      else if (EQUAL(firsttok, "LATT"))
      {
        //Line Attributes (ignored)
      }
      else if (EQUAL(firsttok, "EFLA"))
      {
        end = TRUE;
      }
      else if (EQUAL(firsttok, "ETAB"))
      {
        end = TRUE;
      }
      else
      {
        CPLError(CE_Warning, CPLE_AppDefined, "Unexpected token: %s", firsttok );
      }

      CSLDestroy(tokens);
    }

    //Set feature geometry
    if (eType == wkbMultiCurve)
    {
      feature->SetGeomFieldDirectly(geomIdx, ogrMultiLine);
    }
    else if (eType == wkbMultiLineString)
    {
      feature->SetGeomFieldDirectly(geomIdx, ogrMultiLine->getLinearGeometry());
      delete ogrMultiLine;
    }
    else if (eType == wkbCurvePolygon)
    {
      feature->SetGeomFieldDirectly(geomIdx, ogrPoly);
    }
    else if (eType == wkbPolygon)
    {
      feature->SetGeomFieldDirectly(geomIdx, ogrPoly->getLinearGeometry());
      delete ogrPoly;
    }
    else
    {
      feature->SetGeomFieldDirectly(geomIdx, ogrCurve);
    }
}