OGRCircularString *ILI2Reader::getArc(DOMElement *elem) { // elem -> ARC OGRCircularString *arc = new OGRCircularString(); // previous point -> start point OGRPoint *ptStart = getPoint((DOMElement *)elem->getPreviousSibling()); // COORD or ARC // end point OGRPoint *ptEnd = new OGRPoint(); // point on the arc OGRPoint *ptOnArc = new OGRPoint(); // double radius = 0; // radius DOMElement *arcElem = (DOMElement *)elem->getFirstChild(); while (arcElem != NULL) { char* pszTagName = XMLString::transcode(arcElem->getTagName()); char* pszObjValue = getObjValue(arcElem); if (cmpStr("C1", pszTagName) == 0) ptEnd->setX(CPLAtof(pszObjValue)); else if (cmpStr("C2", pszTagName) == 0) ptEnd->setY(CPLAtof(pszObjValue)); else if (cmpStr("C3", pszTagName) == 0) ptEnd->setZ(CPLAtof(pszObjValue)); else if (cmpStr("A1", pszTagName) == 0) ptOnArc->setX(CPLAtof(pszObjValue)); else if (cmpStr("A2", pszTagName) == 0) ptOnArc->setY(CPLAtof(pszObjValue)); else if (cmpStr("A3", pszTagName) == 0) ptOnArc->setZ(CPLAtof(pszObjValue)); else if (cmpStr("R", pszTagName) == 0) { // radius = CPLAtof(pszObjValue); } CPLFree(pszObjValue); XMLString::release(&pszTagName); arcElem = (DOMElement *)arcElem->getNextSibling(); } arc->addPoint(ptStart); arc->addPoint(ptOnArc); arc->addPoint(ptEnd); delete ptStart; delete ptOnArc; delete ptEnd; return arc; }
OGRLineString *ILI2Reader::getArc(DOMElement *elem) { // elem -> ARC OGRLineString *ls = new OGRLineString(); // previous point -> start point OGRPoint *ptStart = getPoint((DOMElement *)elem->getPreviousSibling()); // COORD or ARC // end point OGRPoint *ptEnd = new OGRPoint(); // point on the arc OGRPoint *ptOnArc = new OGRPoint(); double radius = 0; // radius DOMElement *arcElem = (DOMElement *)elem->getFirstChild(); while (arcElem != NULL) { char* pszTagName = XMLString::transcode(arcElem->getTagName()); char* pszObjValue = getObjValue(arcElem); if (cmpStr("C1", pszTagName) == 0) ptEnd->setX(atof(pszObjValue)); else if (cmpStr("C2", pszTagName) == 0) ptEnd->setY(atof(pszObjValue)); else if (cmpStr("C3", pszTagName) == 0) ptEnd->setZ(atof(pszObjValue)); else if (cmpStr("A1", pszTagName) == 0) ptOnArc->setX(atof(pszObjValue)); else if (cmpStr("A2", pszTagName) == 0) ptOnArc->setY(atof(pszObjValue)); else if (cmpStr("A3", pszTagName) == 0) ptOnArc->setZ(atof(pszObjValue)); else if (cmpStr("R", pszTagName) == 0) radius = atof(pszObjValue); CPLFree(pszObjValue); XMLString::release(&pszTagName); arcElem = (DOMElement *)arcElem->getNextSibling(); } ptEnd->flattenTo2D(); ptOnArc->flattenTo2D(); interpolateArc(ls, ptStart, ptOnArc, ptEnd, arcIncr); delete ptStart; delete ptOnArc; delete ptEnd; return ls; }
static int AddPoint( OGRGeometry *poGeometry, double dfX, double dfY, double dfZ, int nDimension ) { if( poGeometry->getGeometryType() == wkbPoint || poGeometry->getGeometryType() == wkbPoint25D ) { OGRPoint *poPoint = (OGRPoint *) poGeometry; if( poPoint->getX() != 0.0 || poPoint->getY() != 0.0 ) { CPLError( CE_Failure, CPLE_AppDefined, "More than one coordinate for <Point> element."); return FALSE; } poPoint->setX( dfX ); poPoint->setY( dfY ); if( nDimension == 3 ) poPoint->setZ( dfZ ); return TRUE; } else if( poGeometry->getGeometryType() == wkbLineString || poGeometry->getGeometryType() == wkbLineString25D ) { if( nDimension == 3 ) ((OGRLineString *) poGeometry)->addPoint( dfX, dfY, dfZ ); else ((OGRLineString *) poGeometry)->addPoint( dfX, dfY ); return TRUE; } else { CPLAssert( FALSE ); return FALSE; } }
OGRPoint *getPoint(DOMElement *elem) { // elem -> COORD (or ARC) OGRPoint *pt = new OGRPoint(); DOMElement *coordElem = (DOMElement *)elem->getFirstChild(); while (coordElem != NULL) { char* pszTagName = XMLString::transcode(coordElem->getTagName()); char* pszObjValue = getObjValue(coordElem); if (cmpStr("C1", pszTagName) == 0) pt->setX(atof(pszObjValue)); else if (cmpStr("C2", pszTagName) == 0) pt->setY(atof(pszObjValue)); else if (cmpStr("C3", pszTagName) == 0) pt->setZ(atof(pszObjValue)); CPLFree(pszObjValue); XMLString::release(&pszTagName); coordElem = (DOMElement *)coordElem->getNextSibling(); } pt->flattenTo2D(); return pt; }
OGRLineString *getLineString(DOMElement *elem, int bAsLinearRing) { // elem -> POLYLINE OGRLineString *ls; if (bAsLinearRing) ls = new OGRLinearRing(); else ls = new OGRLineString(); DOMElement *lineElem = (DOMElement *)elem->getFirstChild(); while (lineElem != NULL) { char* pszTagName = XMLString::transcode(lineElem->getTagName()); if (cmpStr(ILI2_COORD, pszTagName) == 0) { OGRPoint* poPoint = getPoint(lineElem); ls->addPoint(poPoint); delete poPoint; } else if (cmpStr(ILI2_ARC, pszTagName) == 0) { // end point OGRPoint *ptEnd = new OGRPoint(); // point on the arc OGRPoint *ptOnArc = new OGRPoint(); // radius double radius = 0; DOMElement *arcElem = (DOMElement *)lineElem->getFirstChild(); while (arcElem != NULL) { char* pszTagName = XMLString::transcode(arcElem->getTagName()); char* pszObjValue = getObjValue(arcElem); if (cmpStr("C1", pszTagName) == 0) ptEnd->setX(atof(pszObjValue)); else if (cmpStr("C2", pszTagName) == 0) ptEnd->setY(atof(pszObjValue)); else if (cmpStr("C3", pszTagName) == 0) ptEnd->setZ(atof(pszObjValue)); else if (cmpStr("A1", pszTagName) == 0) ptOnArc->setX(atof(pszObjValue)); else if (cmpStr("A2", pszTagName) == 0) ptOnArc->setY(atof(pszObjValue)); else if (cmpStr("A3", pszTagName) == 0) ptOnArc->setZ(atof(pszObjValue)); else if (cmpStr("R", pszTagName) == 0) radius = atof(pszObjValue); CPLFree(pszObjValue); XMLString::release(&pszTagName); arcElem = (DOMElement *)arcElem->getNextSibling(); } ptEnd->flattenTo2D(); ptOnArc->flattenTo2D(); OGRPoint *ptStart = getPoint((DOMElement *)lineElem->getPreviousSibling()); // COORD or ARC interpolateArc(ls, ptStart, ptOnArc, ptEnd, PI/180); delete ptStart; delete ptEnd; delete ptOnArc; } /* else { // FIXME StructureValue in Polyline not yet supported } */ XMLString::release(&pszTagName); lineElem = (DOMElement *)lineElem->getNextSibling(); } return ls; }
OGRFeature *OGRDODSGridLayer::GetFeature( GIntBig nFeatureId ) { if( nFeatureId < 0 || nFeatureId >= nMaxRawIndex ) return NULL; /* -------------------------------------------------------------------- */ /* Ensure we have the dataset. */ /* -------------------------------------------------------------------- */ if( !ProvideDataDDS() ) return NULL; /* -------------------------------------------------------------------- */ /* Create the feature being read. */ /* -------------------------------------------------------------------- */ OGRFeature *poFeature = new OGRFeature( poFeatureDefn ); poFeature->SetFID( nFeatureId ); m_nFeaturesRead++; /* -------------------------------------------------------------------- */ /* Establish the values for the various dimension indices. */ /* -------------------------------------------------------------------- */ int iDim; int nRemainder = static_cast<int>(nFeatureId); for( iDim = nDimCount-1; iDim >= 0; iDim-- ) { paoDimensions[iDim].iLastValue = (nRemainder % paoDimensions[iDim].nDimEntries) * paoDimensions[iDim].nDimStride + paoDimensions[iDim].nDimStart; nRemainder = nRemainder / paoDimensions[iDim].nDimEntries; if( poTargetGrid == NULL ) poFeature->SetField( iDim, paoDimensions[iDim].iLastValue ); } CPLAssert( nRemainder == 0 ); /* -------------------------------------------------------------------- */ /* For grids, we need to apply the values of the dimensions */ /* looked up in the corresponding map. */ /* -------------------------------------------------------------------- */ if( poTargetGrid != NULL ) { for( iDim = 0; iDim < nDimCount; iDim++ ) { ArrayEntryToField( paoDimensions[iDim].poMap, paoDimensions[iDim].pRawData, paoDimensions[iDim].iLastValue, poFeature, iDim ); } } /* -------------------------------------------------------------------- */ /* Process all the regular data fields. */ /* -------------------------------------------------------------------- */ int iArray; for( iArray = 0; iArray < nArrayRefCount; iArray++ ) { OGRDODSArrayRef *poRef = paoArrayRefs + iArray; ArrayEntryToField( poRef->poArray, poRef->pRawData, static_cast<int>(nFeatureId), poFeature, poRef->iFieldIndex ); } /* -------------------------------------------------------------------- */ /* Do we have geometry information? */ /* -------------------------------------------------------------------- */ if( oXField.iFieldIndex >= 0 && oYField.iFieldIndex >= 0 ) { OGRPoint *poPoint = new OGRPoint(); poPoint->setX( poFeature->GetFieldAsDouble( oXField.iFieldIndex ) ); poPoint->setY( poFeature->GetFieldAsDouble( oYField.iFieldIndex ) ); if( oZField.iFieldIndex >= 0 ) poPoint->setZ( poFeature->GetFieldAsDouble(oZField.iFieldIndex) ); poFeature->SetGeometryDirectly( poPoint ); } return poFeature; }
static OGRCompoundCurve *getPolyline(DOMElement *elem) { // elem -> POLYLINE OGRCompoundCurve *ogrCurve = new OGRCompoundCurve(); OGRLineString *ls = new OGRLineString(); DOMElement *lineElem = (DOMElement *)elem->getFirstChild(); while (lineElem != NULL) { char* pszTagName = XMLString::transcode(lineElem->getTagName()); if (cmpStr(ILI2_COORD, pszTagName) == 0) { OGRPoint* poPoint = getPoint(lineElem); ls->addPoint(poPoint); delete poPoint; } else if (cmpStr(ILI2_ARC, pszTagName) == 0) { //Finish line and start arc if (ls->getNumPoints() > 1) { ogrCurve->addCurveDirectly(ls); ls = new OGRLineString(); } else { ls->empty(); } OGRCircularString *arc = new OGRCircularString(); // end point OGRPoint *ptEnd = new OGRPoint(); // point on the arc OGRPoint *ptOnArc = new OGRPoint(); // radius // double radius = 0; DOMElement *arcElem = (DOMElement *)lineElem->getFirstChild(); while (arcElem != NULL) { char* pszTagName2 = XMLString::transcode(arcElem->getTagName()); char* pszObjValue = getObjValue(arcElem); if (cmpStr("C1", pszTagName2) == 0) ptEnd->setX(CPLAtof(pszObjValue)); else if (cmpStr("C2", pszTagName2) == 0) ptEnd->setY(CPLAtof(pszObjValue)); else if (cmpStr("C3", pszTagName2) == 0) ptEnd->setZ(CPLAtof(pszObjValue)); else if (cmpStr("A1", pszTagName2) == 0) ptOnArc->setX(CPLAtof(pszObjValue)); else if (cmpStr("A2", pszTagName2) == 0) ptOnArc->setY(CPLAtof(pszObjValue)); else if (cmpStr("A3", pszTagName2) == 0) ptOnArc->setZ(CPLAtof(pszObjValue)); else if (cmpStr("R", pszTagName2) == 0) { // radius = CPLAtof(pszObjValue); } CPLFree(pszObjValue); XMLString::release(&pszTagName2); arcElem = (DOMElement *)arcElem->getNextSibling(); } OGRPoint *ptStart = getPoint((DOMElement *)lineElem->getPreviousSibling()); // COORD or ARC arc->addPoint(ptStart); arc->addPoint(ptOnArc); arc->addPoint(ptEnd); ogrCurve->addCurveDirectly(arc); delete ptStart; delete ptEnd; delete ptOnArc; } /* else { // TODO: StructureValue in Polyline not yet supported } */ XMLString::release(&pszTagName); lineElem = (DOMElement *)lineElem->getNextSibling(); } if (ls->getNumPoints() > 1) { ogrCurve->addCurveDirectly(ls); } else { delete ls; } return ogrCurve; }
OGRErr OGRMultiPoint::importFromWkt_Bracketed( char ** ppszInput, int bHasM, int bHasZ ) { /* -------------------------------------------------------------------- */ /* Skip MULTIPOINT keyword. */ /* -------------------------------------------------------------------- */ char szToken[OGR_WKT_TOKEN_MAX] = {}; const char *pszInput = *ppszInput; pszInput = OGRWktReadToken( pszInput, szToken ); if( bHasZ || bHasM ) { // Skip Z, M or ZM. pszInput = OGRWktReadToken( pszInput, szToken ); } /* -------------------------------------------------------------------- */ /* Read points till we get to the closing bracket. */ /* -------------------------------------------------------------------- */ OGRRawPoint *paoPoints = NULL; double *padfZ = NULL; double *padfM = NULL; while( (pszInput = OGRWktReadToken( pszInput, szToken )) != NULL && (EQUAL(szToken, "(") || EQUAL(szToken, ",")) ) { const char* pszNext = OGRWktReadToken( pszInput, szToken ); if( EQUAL(szToken, "EMPTY") ) { OGRPoint *poGeom = new OGRPoint(0.0, 0.0); poGeom->empty(); const OGRErr eErr = addGeometryDirectly( poGeom ); if( eErr != OGRERR_NONE ) { CPLFree( paoPoints ); delete poGeom; return eErr; } pszInput = pszNext; continue; } int flagsFromInput = flags; int nMaxPoint = 0; int nPointCount = 0; pszInput = OGRWktReadPointsM( pszInput, &paoPoints, &padfZ, &padfM, &flagsFromInput, &nMaxPoint, &nPointCount ); if( pszInput == NULL || nPointCount != 1 ) { CPLFree( paoPoints ); CPLFree( padfZ ); CPLFree( padfM ); return OGRERR_CORRUPT_DATA; } if( (flagsFromInput & OGR_G_3D) && !(flags & OGR_G_3D) ) { flags |= OGR_G_3D; bHasZ = TRUE; } if( (flagsFromInput & OGR_G_MEASURED) && !(flags & OGR_G_MEASURED) ) { flags |= OGR_G_MEASURED; bHasM = TRUE; } OGRPoint *poPoint = new OGRPoint(paoPoints[0].x, paoPoints[0].y); if( bHasM ) { if( padfM != NULL ) poPoint->setM(padfM[0]); else poPoint->setM(0.0); } if( bHasZ ) { if( padfZ != NULL ) poPoint->setZ(padfZ[0]); else poPoint->setZ(0.0); } const OGRErr eErr = addGeometryDirectly( poPoint ); if( eErr != OGRERR_NONE ) { CPLFree( paoPoints ); CPLFree( padfZ ); CPLFree( padfM ); delete poPoint; return eErr; } } /* -------------------------------------------------------------------- */ /* Cleanup. */ /* -------------------------------------------------------------------- */ CPLFree( paoPoints ); CPLFree( padfZ ); CPLFree( padfM ); if( !EQUAL(szToken, ")") ) return OGRERR_CORRUPT_DATA; *ppszInput = const_cast<char *>(pszInput); return OGRERR_NONE; }
OGRErr OGRMultiPoint::importFromWkt( char ** ppszInput ) { const char *pszInputBefore = *ppszInput; int bHasZ = FALSE; int bHasM = FALSE; bool bIsEmpty = false; OGRErr eErr = importPreambuleFromWkt(ppszInput, &bHasZ, &bHasM, &bIsEmpty); flags = 0; if( eErr != OGRERR_NONE ) return eErr; if( bHasZ ) flags |= OGR_G_3D; if( bHasM ) flags |= OGR_G_MEASURED; if( bIsEmpty ) return OGRERR_NONE; char szToken[OGR_WKT_TOKEN_MAX] = {}; const char *pszInput = *ppszInput; eErr = OGRERR_NONE; const char* pszPreScan = OGRWktReadToken( pszInput, szToken ); OGRWktReadToken( pszPreScan, szToken ); // Do we have an inner bracket? if( EQUAL(szToken,"(") || EQUAL(szToken, "EMPTY") ) { *ppszInput = const_cast<char *>(pszInputBefore); return importFromWkt_Bracketed( ppszInput, bHasM, bHasZ ); } if( bHasZ || bHasM ) { return OGRERR_CORRUPT_DATA; } /* -------------------------------------------------------------------- */ /* Read the point list which should consist of exactly one point. */ /* -------------------------------------------------------------------- */ OGRRawPoint *paoPoints = NULL; double *padfZ = NULL; double *padfM = NULL; int flagsFromInput = flags; int nMaxPoint = 0; int nPointCount = 0; pszInput = OGRWktReadPointsM( pszInput, &paoPoints, &padfZ, &padfM, &flagsFromInput, &nMaxPoint, &nPointCount ); if( pszInput == NULL ) { CPLFree( paoPoints ); CPLFree( padfZ ); CPLFree( padfM ); return OGRERR_CORRUPT_DATA; } if( (flagsFromInput & OGR_G_3D) && !(flags & OGR_G_3D) ) { flags |= OGR_G_3D; bHasZ = TRUE; } if( (flagsFromInput & OGR_G_MEASURED) && !(flags & OGR_G_MEASURED) ) { flags |= OGR_G_MEASURED; bHasM = TRUE; } /* -------------------------------------------------------------------- */ /* Transform raw points into point objects. */ /* -------------------------------------------------------------------- */ for( int iGeom = 0; iGeom < nPointCount && eErr == OGRERR_NONE; iGeom++ ) { OGRPoint *poPoint = new OGRPoint(paoPoints[iGeom].x, paoPoints[iGeom].y); if( bHasM ) { if( padfM != NULL ) poPoint->setM(padfM[iGeom]); else poPoint->setM(0.0); } if( bHasZ ) { if( padfZ != NULL ) poPoint->setZ(padfZ[iGeom]); else poPoint->setZ(0.0); } eErr = addGeometryDirectly( poPoint ); if( eErr != OGRERR_NONE ) { CPLFree( paoPoints ); CPLFree( padfZ ); CPLFree( padfM ); delete poPoint; return eErr; } } CPLFree( paoPoints ); CPLFree( padfZ ); CPLFree( padfM ); if( eErr != OGRERR_NONE ) return eErr; *ppszInput = const_cast<char *>(pszInput); return OGRERR_NONE; }
OGRFeature *OGRDGNLayer::ElementToFeature( DGNElemCore *psElement ) { OGRFeature *poFeature = new OGRFeature( poFeatureDefn ); poFeature->SetFID( psElement->element_id ); poFeature->SetField( "Type", psElement->type ); poFeature->SetField( "Level", psElement->level ); poFeature->SetField( "GraphicGroup", psElement->graphic_group ); poFeature->SetField( "ColorIndex", psElement->color ); poFeature->SetField( "Weight", psElement->weight ); poFeature->SetField( "Style", psElement->style ); m_nFeaturesRead++; /* -------------------------------------------------------------------- */ /* Collect linkage information */ /* -------------------------------------------------------------------- */ #define MAX_LINK 100 int anEntityNum[MAX_LINK], anMSLink[MAX_LINK]; unsigned char *pabyData; int iLink=0, nLinkCount=0; anEntityNum[0] = 0; anMSLink[0] = 0; pabyData = DGNGetLinkage( hDGN, psElement, iLink, NULL, anEntityNum+iLink, anMSLink+iLink, NULL ); while( pabyData && nLinkCount < MAX_LINK ) { iLink++; if( anEntityNum[nLinkCount] != 0 || anMSLink[nLinkCount] != 0 ) nLinkCount++; anEntityNum[nLinkCount] = 0; anMSLink[nLinkCount] = 0; pabyData = DGNGetLinkage( hDGN, psElement, iLink, NULL, anEntityNum+nLinkCount, anMSLink+nLinkCount, NULL ); } /* -------------------------------------------------------------------- */ /* Apply attribute linkage to feature. */ /* -------------------------------------------------------------------- */ if( nLinkCount > 0 ) { if( EQUAL(pszLinkFormat,"FIRST") ) { poFeature->SetField( "EntityNum", anEntityNum[0] ); poFeature->SetField( "MSLink", anMSLink[0] ); } else if( EQUAL(pszLinkFormat,"LIST") ) { poFeature->SetField( "EntityNum", nLinkCount, anEntityNum ); poFeature->SetField( "MSLink", nLinkCount, anMSLink ); } else if( EQUAL(pszLinkFormat,"STRING") ) { char szEntityList[MAX_LINK*9], szMSLinkList[MAX_LINK*9]; int nEntityLen = 0, nMSLinkLen = 0; for( iLink = 0; iLink < nLinkCount; iLink++ ) { if( iLink != 0 ) { szEntityList[nEntityLen++] = ','; szMSLinkList[nMSLinkLen++] = ','; } sprintf( szEntityList + nEntityLen, "%d", anEntityNum[iLink]); sprintf( szMSLinkList + nMSLinkLen, "%d", anMSLink[iLink] ); nEntityLen += strlen(szEntityList + nEntityLen ); nMSLinkLen += strlen(szMSLinkList + nMSLinkLen ); } poFeature->SetField( "EntityNum", szEntityList ); poFeature->SetField( "MSLink", szMSLinkList ); } } /* -------------------------------------------------------------------- */ /* Lookup color. */ /* -------------------------------------------------------------------- */ char gv_color[128]; int gv_red, gv_green, gv_blue; char szFSColor[128], szPen[256]; szFSColor[0] = '\0'; if( DGNLookupColor( hDGN, psElement->color, &gv_red, &gv_green, &gv_blue ) ) { sprintf( gv_color, "%f %f %f 1.0", gv_red / 255.0, gv_green / 255.0, gv_blue / 255.0 ); sprintf( szFSColor, "c:#%02x%02x%02x", gv_red, gv_green, gv_blue ); } /* -------------------------------------------------------------------- */ /* Generate corresponding PEN style. */ /* -------------------------------------------------------------------- */ if( psElement->style == DGNS_SOLID ) sprintf( szPen, "PEN(id:\"ogr-pen-0\"" ); else if( psElement->style == DGNS_DOTTED ) sprintf( szPen, "PEN(id:\"ogr-pen-5\"" ); else if( psElement->style == DGNS_MEDIUM_DASH ) sprintf( szPen, "PEN(id:\"ogr-pen-2\"" ); else if( psElement->style == DGNS_LONG_DASH ) sprintf( szPen, "PEN(id:\"ogr-pen-4\"" ); else if( psElement->style == DGNS_DOT_DASH ) sprintf( szPen, "PEN(id:\"ogr-pen-6\"" ); else if( psElement->style == DGNS_SHORT_DASH ) sprintf( szPen, "PEN(id:\"ogr-pen-3\"" ); else if( psElement->style == DGNS_DASH_DOUBLE_DOT ) sprintf( szPen, "PEN(id:\"ogr-pen-7\"" ); else if( psElement->style == DGNS_LONG_DASH_SHORT_DASH ) sprintf( szPen, "PEN(p:\"10px 5px 4px 5px\"" ); else sprintf( szPen, "PEN(id:\"ogr-pen-0\"" ); if( strlen(szFSColor) > 0 ) sprintf( szPen+strlen(szPen), ",%s", szFSColor ); if( psElement->weight > 1 ) sprintf( szPen+strlen(szPen), ",w:%dpx", psElement->weight ); strcat( szPen, ")" ); switch( psElement->stype ) { case DGNST_MULTIPOINT: if( psElement->type == DGNT_SHAPE ) { OGRLinearRing *poLine = new OGRLinearRing(); OGRPolygon *poPolygon = new OGRPolygon(); DGNElemMultiPoint *psEMP = (DGNElemMultiPoint *) psElement; poLine->setNumPoints( psEMP->num_vertices ); for( int i = 0; i < psEMP->num_vertices; i++ ) { poLine->setPoint( i, psEMP->vertices[i].x, psEMP->vertices[i].y, psEMP->vertices[i].z ); } poPolygon->addRingDirectly( poLine ); poFeature->SetGeometryDirectly( poPolygon ); ConsiderBrush( psElement, szPen, poFeature ); } else if( psElement->type == DGNT_CURVE ) { DGNElemMultiPoint *psEMP = (DGNElemMultiPoint *) psElement; OGRLineString *poLine = new OGRLineString(); DGNPoint *pasPoints; int nPoints; nPoints = 5 * psEMP->num_vertices; pasPoints = (DGNPoint *) CPLMalloc(sizeof(DGNPoint) * nPoints); DGNStrokeCurve( hDGN, psEMP, nPoints, pasPoints ); poLine->setNumPoints( nPoints ); for( int i = 0; i < nPoints; i++ ) { poLine->setPoint( i, pasPoints[i].x, pasPoints[i].y, pasPoints[i].z ); } poFeature->SetGeometryDirectly( poLine ); CPLFree( pasPoints ); poFeature->SetStyleString( szPen ); } else { OGRLineString *poLine = new OGRLineString(); DGNElemMultiPoint *psEMP = (DGNElemMultiPoint *) psElement; if( psEMP->num_vertices > 0 ) { poLine->setNumPoints( psEMP->num_vertices ); for( int i = 0; i < psEMP->num_vertices; i++ ) { poLine->setPoint( i, psEMP->vertices[i].x, psEMP->vertices[i].y, psEMP->vertices[i].z ); } poFeature->SetGeometryDirectly( poLine ); } poFeature->SetStyleString( szPen ); } break; case DGNST_ARC: { OGRLineString *poLine = new OGRLineString(); DGNElemArc *psArc = (DGNElemArc *) psElement; DGNPoint asPoints[90]; int nPoints; nPoints = (int) (MAX(1,ABS(psArc->sweepang) / 5) + 1); DGNStrokeArc( hDGN, psArc, nPoints, asPoints ); poLine->setNumPoints( nPoints ); for( int i = 0; i < nPoints; i++ ) { poLine->setPoint( i, asPoints[i].x, asPoints[i].y, asPoints[i].z ); } poFeature->SetGeometryDirectly( poLine ); poFeature->SetStyleString( szPen ); } break; case DGNST_TEXT: { OGRPoint *poPoint = new OGRPoint(); DGNElemText *psText = (DGNElemText *) psElement; char *pszOgrFS; poPoint->setX( psText->origin.x ); poPoint->setY( psText->origin.y ); poPoint->setZ( psText->origin.z ); poFeature->SetGeometryDirectly( poPoint ); pszOgrFS = (char *) CPLMalloc(strlen(psText->string) + 150); // setup the basic label. sprintf( pszOgrFS, "LABEL(t:\"%s\"", psText->string ); // set the color if we have it. if( strlen(szFSColor) > 0 ) sprintf( pszOgrFS+strlen(pszOgrFS), ",%s", szFSColor ); // Add the size info in ground units. if( ABS(psText->height_mult) >= 6.0 ) sprintf( pszOgrFS+strlen(pszOgrFS), ",s:%dg", (int) psText->height_mult ); else if( ABS(psText->height_mult) > 0.1 ) sprintf( pszOgrFS+strlen(pszOgrFS), ",s:%.3fg", psText->height_mult ); else sprintf( pszOgrFS+strlen(pszOgrFS), ",s:%.12fg", psText->height_mult ); // Add the font name. Name it MstnFont<FONTNUMBER> if not available // in the font list. #3392 static const char *papszFontList[] = { "STANDARD", "WORKING", "FANCY", "ENGINEERING", "NEWZERO", "STENCEL", //0-5 "USTN_FANCY", "COMPRESSED", "STENCEQ", NULL, "hand", "ARCH", //6-11 "ARCHB", NULL, NULL, "IGES1001", "IGES1002", "IGES1003", //12-17 "CENTB", "MICROS", NULL, NULL, "ISOFRACTIONS", "ITALICS", //18-23 "ISO30", NULL, "GREEK", "ISOREC", "Isoeq", NULL, //24-29 "ISO_FONTLEFT", "ISO_FONTRIGHT", "INTL_ENGINEERING", "INTL_WORKING", "ISOITEQ", NULL, //30-35 "USTN FONT 26", NULL, NULL, NULL, NULL, "ARCHITECTURAL", //36-41 "BLOCK_OUTLINE", "LOW_RES_FILLED", NULL, NULL, NULL, NULL, //42-47 NULL, NULL, "UPPERCASE", NULL, NULL, NULL, //48-53 NULL, NULL, NULL, NULL, NULL, NULL, //54-49 "FONT060", "din", "dinit", "helvl", "HELVLIT", "helv", //60-65 "HELVIT", "cent", "CENTIT", "SCRIPT", NULL, NULL, //66-71 NULL, NULL, NULL, NULL, "MICROQ", "dotfont", //72-77 "DOTIT", NULL, NULL, NULL, NULL, NULL, //78-83 NULL, NULL, NULL, NULL, NULL, NULL, //84-89 NULL, NULL, "FONT092", NULL, "FONT094", NULL, //90-95 NULL, NULL, NULL, NULL, "ANSI_SYMBOLS", "FEATURE_CONTROL_SYSMBOLS", //96-101 "SYMB_FAST", NULL, NULL, "INTL_ISO", "INTL_ISO_EQUAL", "INTL_ISO_ITALIC", //102-107 "INTL_ISO_ITALIC_EQUAL" }; //108 if(psText->font_id <= 108 && papszFontList[psText->font_id] != NULL ) { sprintf( pszOgrFS+strlen(pszOgrFS), ",f:%s", papszFontList[psText->font_id] ); } else { sprintf( pszOgrFS+strlen(pszOgrFS), ",f:MstnFont%d", psText->font_id ); } // Add the angle, if not horizontal if( psText->rotation != 0.0 ) sprintf( pszOgrFS+strlen(pszOgrFS), ",a:%d", (int) (psText->rotation+0.5) ); strcat( pszOgrFS, ")" ); poFeature->SetStyleString( pszOgrFS ); CPLFree( pszOgrFS ); poFeature->SetField( "Text", psText->string ); } break; case DGNST_COMPLEX_HEADER: { DGNElemComplexHeader *psHdr = (DGNElemComplexHeader *) psElement; int iChild; OGRMultiLineString oChildren; /* collect subsequent child geometries. */ // we should disable the spatial filter ... add later. for( iChild = 0; iChild < psHdr->numelems; iChild++ ) { OGRFeature *poChildFeature = NULL; DGNElemCore *psChildElement; psChildElement = DGNReadElement( hDGN ); // should verify complex bit set, not another header. if( psChildElement != NULL ) { poChildFeature = ElementToFeature( psChildElement ); DGNFreeElement( hDGN, psChildElement ); } if( poChildFeature != NULL && poChildFeature->GetGeometryRef() != NULL ) { OGRGeometry *poGeom; poGeom = poChildFeature->GetGeometryRef(); if( wkbFlatten(poGeom->getGeometryType()) == wkbLineString ) oChildren.addGeometry( poGeom ); } if( poChildFeature != NULL ) delete poChildFeature; } // Try to assemble into polygon geometry. OGRGeometry *poGeom; if( psElement->type == DGNT_COMPLEX_SHAPE_HEADER ) poGeom = (OGRPolygon *) OGRBuildPolygonFromEdges( (OGRGeometryH) &oChildren, TRUE, TRUE, 100000, NULL ); else poGeom = oChildren.clone(); if( poGeom != NULL ) poFeature->SetGeometryDirectly( poGeom ); ConsiderBrush( psElement, szPen, poFeature ); } break; default: break; } /* -------------------------------------------------------------------- */ /* Fixup geometry dimension. */ /* -------------------------------------------------------------------- */ if( poFeature->GetGeometryRef() != NULL ) poFeature->GetGeometryRef()->setCoordinateDimension( DGNGetDimension( hDGN ) ); return poFeature; }
bool Shape::save(const std::string& filename) { if (shapeType == -1) { std::cout << "Shape type is not set." << std::endl; return false; } if (shapeObjects.size() == 0) { std::cout << "No shape exists." << std::endl; return false; } const char *pszDriverName = "ESRI Shapefile"; GDALDriver *poDriver; GDALAllRegister(); poDriver = GetGDALDriverManager()->GetDriverByName(pszDriverName); if (poDriver == NULL) { printf("%s driver not available.\n", pszDriverName); return false; } GDALDataset *poDS; poDS = poDriver->Create(filename.c_str(), 0, 0, 0, GDT_Unknown, NULL); if (poDS == NULL) { printf("Creation of output file failed.\n"); return false; } OGRLayer *poLayer; if (shapeType == wkbPoint) { poLayer = poDS->CreateLayer("point_out", NULL, wkbPoint, NULL); } else if (shapeType == wkbLineString) { poLayer = poDS->CreateLayer("point_out", NULL, wkbLineString, NULL); } else if (shapeType == wkbPolygon) { poLayer = poDS->CreateLayer("point_out", NULL, wkbPolygon, NULL); } if (poLayer == NULL) { printf("Layer creation failed.\n"); return false; } for (auto it = shapeObjects[0].attributes.begin(); it != shapeObjects[0].attributes.end(); ++it) { OGRFieldDefn oField(it->first.c_str(), static_cast<OGRFieldType>(it->second.type)); if (it->second.type == OFTString) { oField.SetWidth(it->second.stringValue().size()); } if (poLayer->CreateField(&oField) != OGRERR_NONE) { printf("Creating Name field failed.\n"); return false; } } for (int i = 0; i < shapeObjects.size(); ++i) { if (shapeObjects[i].parts.size() == 0) continue; OGRFeature *poFeature; poFeature = OGRFeature::CreateFeature(poLayer->GetLayerDefn()); // 属性をセット for (auto it = shapeObjects[i].attributes.begin(); it != shapeObjects[i].attributes.end(); ++it) { poFeature->SetField(it->first.c_str(), it->second.stringValue().c_str()); } // ジオメトリ情報をセット if (shapeType == wkbPoint) { OGRPoint point; point.setX(shapeObjects[i].parts[0].points[0].x); point.setY(shapeObjects[i].parts[0].points[0].y); point.setZ(shapeObjects[i].parts[0].points[0].z); poFeature->SetGeometry(&point); } else if (shapeType == wkbLineString) { OGRLineString lineString; for (int k = 0; k < shapeObjects[i].parts[0].points.size(); ++k) { lineString.addPoint(shapeObjects[i].parts[0].points[k].x, shapeObjects[i].parts[0].points[k].y, shapeObjects[i].parts[0].points[k].z); } poFeature->SetGeometry(&lineString); } else if (shapeType == wkbPolygon) { OGRPolygon polygon; for (int j = 0; j < shapeObjects[i].parts.size(); ++j) { OGRLinearRing linearRing; for (int k = 0; k < shapeObjects[i].parts[j].points.size(); ++k) { linearRing.addPoint(shapeObjects[i].parts[j].points[k].x, shapeObjects[i].parts[j].points[k].y, shapeObjects[i].parts[j].points[k].z); } polygon.addRing(&linearRing); } poFeature->SetGeometry(&polygon); } if (poLayer->CreateFeature(poFeature) != OGRERR_NONE) { printf("Failed to create feature in shapefile.\n"); return false; } OGRFeature::DestroyFeature(poFeature); } GDALClose(poDS); return true; }
OGRFeature *OGRCADLayer::GetFeature( GIntBig nFID ) { if( poCADLayer.getGeometryCount() <= static_cast<size_t>(nFID) || nFID < 0 ) { return nullptr; } OGRFeature *poFeature = nullptr; CADGeometry *poCADGeometry = poCADLayer.getGeometry( static_cast<size_t>(nFID) ); if( nullptr == poCADGeometry || GetLastErrorCode() != CADErrorCodes::SUCCESS ) { CPLError( CE_Failure, CPLE_NotSupported, "Failed to get geometry with ID = " CPL_FRMT_GIB " from layer \"%s\". Libopencad errorcode: %d", nFID, poCADLayer.getName().c_str(), GetLastErrorCode() ); return nullptr; } poFeature = new OGRFeature( poFeatureDefn ); poFeature->SetFID( nFID ); poFeature->SetField( FIELD_NAME_THICKNESS, poCADGeometry->getThickness() ); if( !poCADGeometry->getEED().empty() ) { std::vector<std::string> asGeometryEED = poCADGeometry->getEED(); std::string sEEDAsOneString = ""; for ( std::vector<std::string>::const_iterator iter = asGeometryEED.cbegin(); iter != asGeometryEED.cend(); ++iter ) { sEEDAsOneString += *iter; sEEDAsOneString += ' '; } poFeature->SetField( FIELD_NAME_EXT_DATA, sEEDAsOneString.c_str() ); } RGBColor stRGB = poCADGeometry->getColor(); CPLString sHexColor; sHexColor.Printf("#%02X%02X%02X%02X", stRGB.R, stRGB.G, stRGB.B, 255); poFeature->SetField( FIELD_NAME_COLOR, sHexColor ); CPLString sStyle; sStyle.Printf("PEN(c:%s,w:5px)", sHexColor.c_str()); poFeature->SetStyleString( sStyle ); std::vector< CADAttrib > oBlockAttrs = poCADGeometry->getBlockAttributes(); for( const CADAttrib& oAttrib : oBlockAttrs ) { CPLString osTag = oAttrib.getTag(); auto featureAttrIt = asFeaturesAttributes.find( osTag ); if( featureAttrIt != asFeaturesAttributes.end()) { poFeature->SetField(*featureAttrIt, oAttrib.getTextValue().c_str()); } } switch( poCADGeometry->getType() ) { case CADGeometry::POINT: { CADPoint3D * const poCADPoint = ( CADPoint3D* ) poCADGeometry; CADVector stPositionVector = poCADPoint->getPosition(); poFeature->SetGeometryDirectly( new OGRPoint( stPositionVector.getX(), stPositionVector.getY(), stPositionVector.getZ() ) ); poFeature->SetField( FIELD_NAME_GEOMTYPE, "CADPoint" ); break; } case CADGeometry::LINE: { CADLine * const poCADLine = ( CADLine* ) poCADGeometry; OGRLineString *poLS = new OGRLineString(); poLS->addPoint( poCADLine->getStart().getPosition().getX(), poCADLine->getStart().getPosition().getY(), poCADLine->getStart().getPosition().getZ() ); poLS->addPoint( poCADLine->getEnd().getPosition().getX(), poCADLine->getEnd().getPosition().getY(), poCADLine->getEnd().getPosition().getZ() ); poFeature->SetGeometryDirectly( poLS ); poFeature->SetField( FIELD_NAME_GEOMTYPE, "CADLine" ); break; } case CADGeometry::SOLID: { CADSolid * const poCADSolid = ( CADSolid* ) poCADGeometry; OGRPolygon * poPoly = new OGRPolygon(); OGRLinearRing * poLR = new OGRLinearRing(); std::vector<CADVector> astSolidCorners = poCADSolid->getCorners(); for( size_t i = 0; i < astSolidCorners.size(); ++i ) { poLR->addPoint( astSolidCorners[i].getX(), astSolidCorners[i].getY(), astSolidCorners[i].getZ()); } poPoly->addRingDirectly( poLR ); poPoly->closeRings(); poFeature->SetGeometryDirectly( poPoly ); poFeature->SetField( FIELD_NAME_GEOMTYPE, "CADSolid" ); break; } case CADGeometry::CIRCLE: { CADCircle * poCADCircle = static_cast<CADCircle*>(poCADGeometry); OGRCircularString * poCircle = new OGRCircularString(); CADVector stCircleCenter = poCADCircle->getPosition(); OGRPoint oCirclePoint1; oCirclePoint1.setX( stCircleCenter.getX() - poCADCircle->getRadius() ); oCirclePoint1.setY( stCircleCenter.getY() ); oCirclePoint1.setZ( stCircleCenter.getZ() ); poCircle->addPoint( &oCirclePoint1 ); OGRPoint oCirclePoint2; oCirclePoint2.setX( stCircleCenter.getX() ); oCirclePoint2.setY( stCircleCenter.getY() + poCADCircle->getRadius() ); oCirclePoint2.setZ( stCircleCenter.getZ() ); poCircle->addPoint( &oCirclePoint2 ); OGRPoint oCirclePoint3; oCirclePoint3.setX( stCircleCenter.getX() + poCADCircle->getRadius() ); oCirclePoint3.setY( stCircleCenter.getY() ); oCirclePoint3.setZ( stCircleCenter.getZ() ); poCircle->addPoint( &oCirclePoint3 ); OGRPoint oCirclePoint4; oCirclePoint4.setX( stCircleCenter.getX() ); oCirclePoint4.setY( stCircleCenter.getY() - poCADCircle->getRadius() ); oCirclePoint4.setZ( stCircleCenter.getZ() ); poCircle->addPoint( &oCirclePoint4 ); // Close the circle poCircle->addPoint( &oCirclePoint1 ); /*NOTE: The alternative way: OGRGeometry *poCircle = OGRGeometryFactory::approximateArcAngles( poCADCircle->getPosition().getX(), poCADCircle->getPosition().getY(), poCADCircle->getPosition().getZ(), poCADCircle->getRadius(), poCADCircle->getRadius(), 0.0, 0.0, 360.0, 0.0 ); */ poFeature->SetGeometryDirectly( poCircle ); poFeature->SetField( FIELD_NAME_GEOMTYPE, "CADCircle" ); break; } case CADGeometry::ARC: { CADArc * poCADArc = static_cast<CADArc*>(poCADGeometry); OGRCircularString * poCircle = new OGRCircularString(); // Need at least 3 points in arc double dfStartAngle = poCADArc->getStartingAngle() * DEG2RAD; double dfEndAngle = poCADArc->getEndingAngle() * DEG2RAD; double dfMidAngle = (dfEndAngle + dfStartAngle) / 2; CADVector stCircleCenter = poCADArc->getPosition(); OGRPoint oCirclePoint; oCirclePoint.setX( stCircleCenter.getX() + poCADArc->getRadius() * cos( dfStartAngle ) ); oCirclePoint.setY( stCircleCenter.getY() + poCADArc->getRadius() * sin( dfStartAngle ) ); oCirclePoint.setZ( stCircleCenter.getZ() ); poCircle->addPoint( &oCirclePoint ); oCirclePoint.setX( stCircleCenter.getX() + poCADArc->getRadius() * cos( dfMidAngle ) ); oCirclePoint.setY( stCircleCenter.getY() + poCADArc->getRadius() * sin( dfMidAngle ) ); oCirclePoint.setZ( stCircleCenter.getZ() ); poCircle->addPoint( &oCirclePoint ); oCirclePoint.setX( stCircleCenter.getX() + poCADArc->getRadius() * cos( dfEndAngle ) ); oCirclePoint.setY( stCircleCenter.getY() + poCADArc->getRadius() * sin( dfEndAngle ) ); oCirclePoint.setZ( stCircleCenter.getZ() ); poCircle->addPoint( &oCirclePoint ); /*NOTE: alternative way: OGRGeometry * poArc = OGRGeometryFactory::approximateArcAngles( poCADArc->getPosition().getX(), poCADArc->getPosition().getY(), poCADArc->getPosition().getZ(), poCADArc->getRadius(), poCADArc->getRadius(), 0.0, dfStartAngle, dfStartAngle > dfEndAngle ? ( dfEndAngle + 360.0f ) : dfEndAngle, 0.0 ); */ poFeature->SetGeometryDirectly( poCircle ); poFeature->SetField( FIELD_NAME_GEOMTYPE, "CADArc" ); break; } case CADGeometry::FACE3D: { CADFace3D * const poCADFace = ( CADFace3D* ) poCADGeometry; OGRPolygon * poPoly = new OGRPolygon(); OGRLinearRing * poLR = new OGRLinearRing(); for ( size_t i = 0; i < 3; ++i ) { poLR->addPoint( poCADFace->getCorner( i ).getX(), poCADFace->getCorner( i ).getY(), poCADFace->getCorner( i ).getZ() ); } if ( !(poCADFace->getCorner( 2 ) == poCADFace->getCorner( 3 )) ) { poLR->addPoint( poCADFace->getCorner( 3 ).getX(), poCADFace->getCorner( 3 ).getY(), poCADFace->getCorner( 3 ).getZ() ); } poPoly->addRingDirectly( poLR ); poPoly->closeRings(); poFeature->SetGeometryDirectly( poPoly ); poFeature->SetField( FIELD_NAME_GEOMTYPE, "CADFace3D" ); break; } case CADGeometry::LWPOLYLINE: { CADLWPolyline * const poCADLWPolyline = ( CADLWPolyline* ) poCADGeometry; poFeature->SetField( FIELD_NAME_GEOMTYPE, "CADLWPolyline" ); /* * Excessive check, like in DXF driver. * I tried to make a single-point polyline, but couldn't make it. * Probably this check should be removed. */ if( poCADLWPolyline->getVertexCount() == 1 ) { poFeature->SetGeometryDirectly( new OGRPoint( poCADLWPolyline->getVertex(0).getX(), poCADLWPolyline->getVertex(0).getY(), poCADLWPolyline->getVertex(0).getZ() ) ); break; } /* * If polyline has no arcs, handle it in easy way. */ OGRLineString * poLS = new OGRLineString(); if( poCADLWPolyline->getBulges().empty() ) { for( size_t i = 0; i < poCADLWPolyline->getVertexCount(); ++i ) { CADVector stVertex = poCADLWPolyline->getVertex( i ); poLS->addPoint( stVertex.getX(), stVertex.getY(), stVertex.getZ() ); } poFeature->SetGeometryDirectly( poLS ); break; } /* * Last case - if polyline has mixed arcs and lines. */ bool bLineStringStarted = false; std::vector< double > adfBulges = poCADLWPolyline->getBulges(); const size_t nCount = std::min(adfBulges.size(), poCADLWPolyline->getVertexCount()); for( size_t iCurrentVertex = 0; iCurrentVertex + 1 < nCount; iCurrentVertex++ ) { CADVector stCurrentVertex = poCADLWPolyline->getVertex( iCurrentVertex ); CADVector stNextVertex = poCADLWPolyline->getVertex( iCurrentVertex + 1 ); double dfLength = sqrt( pow( stNextVertex.getX() - stCurrentVertex.getX(), 2 ) + pow( stNextVertex.getY() - stCurrentVertex.getY(), 2 ) ); /* * Handling straight polyline segment. */ if( ( dfLength == 0 ) || ( adfBulges[iCurrentVertex] == 0 ) ) { if( !bLineStringStarted ) { poLS->addPoint( stCurrentVertex.getX(), stCurrentVertex.getY(), stCurrentVertex.getZ() ); bLineStringStarted = true; } poLS->addPoint( stNextVertex.getX(), stNextVertex.getY(), stNextVertex.getZ() ); } else { double dfSegmentBulge = adfBulges[iCurrentVertex]; double dfH = ( dfSegmentBulge * dfLength ) / 2; if( dfH == 0.0 ) dfH = 1.0; // just to avoid a division by zero double dfRadius = ( dfH / 2 ) + ( dfLength * dfLength / ( 8 * dfH ) ); double dfOgrArcRotation = 0, dfOgrArcRadius = fabs( dfRadius ); /* * Set arc's direction and keep bulge positive. */ bool bClockwise = ( dfSegmentBulge < 0 ); if( bClockwise ) dfSegmentBulge *= -1; /* * Get arc's center point. */ double dfSaggita = fabs( dfSegmentBulge * ( dfLength / 2.0 ) ); double dfApo = bClockwise ? -( dfOgrArcRadius - dfSaggita ) : -( dfSaggita - dfOgrArcRadius ); CADVector stVertex; stVertex.setX( stCurrentVertex.getX() - stNextVertex.getX() ); stVertex.setY( stCurrentVertex.getY() - stNextVertex.getY() ); stVertex.setZ( stCurrentVertex.getZ() ); CADVector stMidPoint; stMidPoint.setX( stNextVertex.getX() + 0.5 * stVertex.getX() ); stMidPoint.setY( stNextVertex.getY() + 0.5 * stVertex.getY() ); stMidPoint.setZ( stVertex.getZ() ); CADVector stPperp; stPperp.setX( stVertex.getY() ); stPperp.setY( -stVertex.getX() ); double dfStPperpLength = sqrt( stPperp.getX() * stPperp.getX() + stPperp.getY() * stPperp.getY() ); // TODO: Check that length isnot 0 stPperp.setX( stPperp.getX() / dfStPperpLength ); stPperp.setY( stPperp.getY() / dfStPperpLength ); CADVector stOgrArcCenter; stOgrArcCenter.setX( stMidPoint.getX() + ( stPperp.getX() * dfApo ) ); stOgrArcCenter.setY( stMidPoint.getY() + ( stPperp.getY() * dfApo ) ); /* * Get the line's general vertical direction ( -1 = down, +1 = up ). */ double dfLineDir = stNextVertex.getY() > stCurrentVertex.getY() ? 1.0f : -1.0f; /* * Get arc's starting angle. */ double dfA = atan2( ( stOgrArcCenter.getY() - stCurrentVertex.getY() ), ( stOgrArcCenter.getX() - stCurrentVertex.getX() ) ) * DEG2RAD; if( bClockwise && ( dfLineDir == 1.0 ) ) dfA += ( dfLineDir * 180.0 ); double dfOgrArcStartAngle = dfA > 0.0 ? -( dfA - 180.0 ) : -( dfA + 180.0 ); /* * Get arc's ending angle. */ dfA = atan2( ( stOgrArcCenter.getY() - stNextVertex.getY() ), ( stOgrArcCenter.getX() - stNextVertex.getX() ) ) * DEG2RAD; if( bClockwise && ( dfLineDir == 1.0 ) ) dfA += ( dfLineDir * 180.0 ); double dfOgrArcEndAngle = dfA > 0.0 ? -( dfA - 180.0 ) : -( dfA + 180.0 ); if( !bClockwise && ( dfOgrArcStartAngle < dfOgrArcEndAngle) ) dfOgrArcEndAngle = -180.0 + ( dfLineDir * dfA ); if( bClockwise && ( dfOgrArcStartAngle > dfOgrArcEndAngle ) ) dfOgrArcEndAngle += 360.0; /* * Flip arc's rotation if necessary. */ if( bClockwise && ( dfLineDir == 1.0 ) ) dfOgrArcRotation = dfLineDir * 180.0; /* * Tessellate the arc segment and append to the linestring. */ OGRLineString * poArcpoLS = ( OGRLineString * ) OGRGeometryFactory::approximateArcAngles( stOgrArcCenter.getX(), stOgrArcCenter.getY(), stOgrArcCenter.getZ(), dfOgrArcRadius, dfOgrArcRadius, dfOgrArcRotation, dfOgrArcStartAngle,dfOgrArcEndAngle, 0.0 ); poLS->addSubLineString( poArcpoLS ); delete( poArcpoLS ); } } if( poCADLWPolyline->isClosed() ) { poLS->addPoint( poCADLWPolyline->getVertex(0).getX(), poCADLWPolyline->getVertex(0).getY(), poCADLWPolyline->getVertex(0).getZ() ); } poFeature->SetGeometryDirectly( poLS ); poFeature->SetField( FIELD_NAME_GEOMTYPE, "CADLWPolyline" ); break; } // TODO: Unsupported smooth lines case CADGeometry::POLYLINE3D: { CADPolyline3D * const poCADPolyline3D = ( CADPolyline3D* ) poCADGeometry; OGRLineString * poLS = new OGRLineString(); for( size_t i = 0; i < poCADPolyline3D->getVertexCount(); ++i ) { CADVector stVertex = poCADPolyline3D->getVertex( i ); poLS->addPoint( stVertex.getX(), stVertex.getY(), stVertex.getZ() ); } poFeature->SetGeometryDirectly( poLS ); poFeature->SetField( FIELD_NAME_GEOMTYPE, "CADPolyline3D" ); break; } case CADGeometry::TEXT: { CADText * const poCADText = ( CADText * ) poCADGeometry; OGRPoint * poPoint = new OGRPoint( poCADText->getPosition().getX(), poCADText->getPosition().getY(), poCADText->getPosition().getZ() ); CPLString sTextValue = CADRecode( poCADText->getTextValue(), nDWGEncoding ); poFeature->SetField( FIELD_NAME_TEXT, sTextValue ); poFeature->SetGeometryDirectly( poPoint ); poFeature->SetField( FIELD_NAME_GEOMTYPE, "CADText" ); sStyle.Printf("LABEL(f:\"Arial\",t:\"%s\",c:%s)", sTextValue.c_str(), sHexColor.c_str()); poFeature->SetStyleString( sStyle ); break; } case CADGeometry::MTEXT: { CADMText * const poCADMText = ( CADMText * ) poCADGeometry; OGRPoint * poPoint = new OGRPoint( poCADMText->getPosition().getX(), poCADMText->getPosition().getY(), poCADMText->getPosition().getZ() ); CPLString sTextValue = CADRecode( poCADMText->getTextValue(), nDWGEncoding ); poFeature->SetField( FIELD_NAME_TEXT, sTextValue ); poFeature->SetGeometryDirectly( poPoint ); poFeature->SetField( FIELD_NAME_GEOMTYPE, "CADMText" ); sStyle.Printf("LABEL(f:\"Arial\",t:\"%s\",c:%s)", sTextValue.c_str(), sHexColor.c_str()); poFeature->SetStyleString( sStyle ); break; } case CADGeometry::SPLINE: { CADSpline * const poCADSpline = ( CADSpline * ) poCADGeometry; OGRLineString * poLS = new OGRLineString(); // TODO: Interpolate spline as points or curves for( size_t i = 0; i < poCADSpline->getControlPoints().size(); ++i ) { poLS->addPoint( poCADSpline->getControlPoints()[i].getX(), poCADSpline->getControlPoints()[i].getY(), poCADSpline->getControlPoints()[i].getZ() ); } poFeature->SetGeometryDirectly( poLS ); poFeature->SetField( FIELD_NAME_GEOMTYPE, "CADSpline" ); break; } case CADGeometry::ELLIPSE: { CADEllipse * poCADEllipse = static_cast<CADEllipse*>(poCADGeometry); // FIXME: Start/end angles should be swapped to work exactly as DXF driver. // is it correct? double dfStartAngle = -1 * poCADEllipse->getEndingAngle() * DEG2RAD; double dfEndAngle = -1 * poCADEllipse->getStartingAngle() * DEG2RAD; double dfAxisRatio = poCADEllipse->getAxisRatio(); dfStartAngle = fmod(dfStartAngle, 360.0); dfEndAngle = fmod(dfEndAngle, 360.0); if( dfStartAngle > dfEndAngle ) dfEndAngle += 360.0; CADVector vectPosition = poCADEllipse->getPosition(); CADVector vectSMAxis = poCADEllipse->getSMAxis(); double dfPrimaryRadius, dfSecondaryRadius; double dfRotation; dfPrimaryRadius = sqrt( vectSMAxis.getX() * vectSMAxis.getX() + vectSMAxis.getY() * vectSMAxis.getY() + vectSMAxis.getZ() * vectSMAxis.getZ() ); dfSecondaryRadius = dfAxisRatio * dfPrimaryRadius; dfRotation = -1 * atan2( vectSMAxis.getY(), vectSMAxis.getX() ) * DEG2RAD; OGRGeometry *poEllipse = OGRGeometryFactory::approximateArcAngles( vectPosition.getX(), vectPosition.getY(), vectPosition.getZ(), dfPrimaryRadius, dfSecondaryRadius, dfRotation, dfStartAngle, dfEndAngle, 0.0 ); poFeature->SetGeometryDirectly( poEllipse ); poFeature->SetField( FIELD_NAME_GEOMTYPE, "CADEllipse" ); break; } case CADGeometry::ATTDEF: { CADAttdef * const poCADAttdef = ( CADAttdef* ) poCADGeometry; OGRPoint * poPoint = new OGRPoint( poCADAttdef->getPosition().getX(), poCADAttdef->getPosition().getY(), poCADAttdef->getPosition().getZ() ); CPLString sTextValue = CADRecode( poCADAttdef->getTag(), nDWGEncoding ); poFeature->SetField( FIELD_NAME_TEXT, sTextValue ); poFeature->SetGeometryDirectly( poPoint ); poFeature->SetField( FIELD_NAME_GEOMTYPE, "CADAttdef" ); sStyle.Printf("LABEL(f:\"Arial\",t:\"%s\",c:%s)", sTextValue.c_str(), sHexColor.c_str()); poFeature->SetStyleString( sStyle ); break; } default: { CPLError( CE_Warning, CPLE_NotSupported, "Unhandled feature. Skipping it." ); poFeature->SetField( FIELD_NAME_GEOMTYPE, "CADUnknown" ); delete poCADGeometry; return poFeature; } } delete poCADGeometry; poFeature->GetGeometryRef()->assignSpatialReference( poSpatialRef ); return poFeature; }