示例#1
0
OGRErr OGRLIBKMLLayer::ISetFeature ( OGRFeature * poOgrFeat )
{
    if( !bUpdate || m_poKmlUpdate == NULL )
        return OGRERR_UNSUPPORTED_OPERATION;
    if( poOgrFeat->GetFID() == OGRNullFID )
        return OGRERR_FAILURE;

    FeaturePtr poKmlFeature =
        feat2kml ( m_poOgrDS, this, poOgrFeat, m_poOgrDS->GetKmlFactory (  ),
                   m_bUseSimpleField );

    KmlFactory *poKmlFactory = m_poOgrDS->GetKmlFactory (  );
    ChangePtr poChange = poKmlFactory->CreateChange();
    poChange->add_object(poKmlFeature);
    m_poKmlUpdate->add_updateoperation(poChange);

    const char* pszId = CPLSPrintf("%s." CPL_FRMT_GIB,
                    OGRLIBKMLGetSanitizedNCName(GetName()).c_str(), poOgrFeat->GetFID());
    poKmlFeature->set_targetid(pszId);

    /***** mark the layer as updated *****/

    bUpdated = TRUE;
    m_poOgrDS->Updated (  );

    return OGRERR_NONE;
}
示例#2
0
  bool UAVFlightPlan::exportToKMLFile(const std::string &f, int begin_, int end_) const {
    bool ret_val = true;
    
    if (end_ < 0 || end_ > size()) {
      end_ = size(); 
    }
  
    if (begin_ < 0 || begin_ > size()) {
      begin_ = 0;
    }
    
    const_iterator it = begin();
    
    // Advance to the first waypoint to save
    int i = 0;
    for (; i < begin_; i++, it++);

    KmlFactory* factory = KmlFactory::GetFactory();
    
    
		kmldom::DocumentPtr doc = factory->CreateDocument();
		
    for (int j = 1; i < end_ && it != end(); j++,i++, it++) {
			ostringstream name_;
			name_ << "Waypoint " << j;
			// Create a <Point> with <coordinates> from the given Vec3.
			kmlbase::Vec3 v(it->getLongitude(), it->getLatitude(), it->getAltitude());
			kmldom::PointPtr point = kmlconvenience::CreatePointFromVec3(v);
			PlacemarkPtr place = factory->CreatePlacemark();
			place->set_geometry(point);
			doc->add_feature(place);
    }
    
    
    
    // Finally create the kml
    KmlPtr kml = factory->CreateKml();
    kml->set_feature(doc);
    
    // Then the file
    KmlFilePtr kmlfile = KmlFile::CreateFromImport(kml);
    if (!kmlfile) {
      cerr << "error: could not create kml file" << endl;
      return false;
    }
    
    // And write it
    std::string kml_data;
    kmlfile->SerializeToString(&kml_data);
    if (!kmlbase::File::WriteStringToFile(kml_data, f.c_str())) {
      cerr << "error: write of " << f << " failed" << endl;
      ret_val = false;
    }
    
    return ret_val;
  }
示例#3
0
void OGRLIBKMLLayer::SetLookAt( const char* pszLookatLongitude,
                                const char* pszLookatLatitude,
                                const char* pszLookatAltitude,
                                const char* pszLookatHeading,
                                const char* pszLookatTilt,
                                const char* pszLookatRange,
                                const char* pszLookatAltitudeMode )
{
    KmlFactory *poKmlFactory = m_poOgrDS->GetKmlFactory();
    LookAtPtr lookAt = poKmlFactory->CreateLookAt();
    lookAt->set_latitude(CPLAtof(pszLookatLatitude));
    lookAt->set_longitude(CPLAtof(pszLookatLongitude));
    if( pszLookatAltitude != NULL )
        lookAt->set_altitude(CPLAtof(pszLookatAltitude));
    if( pszLookatHeading != NULL )
        lookAt->set_heading(CPLAtof(pszLookatHeading));
    if( pszLookatTilt != NULL )
    {
        double dfTilt = CPLAtof(pszLookatTilt);
        if( dfTilt >= 0 && dfTilt <= 90 )
            lookAt->set_tilt(dfTilt);
        else
            CPLError(CE_Warning, CPLE_AppDefined, "Invalid value for tilt: %s",
                     pszLookatTilt);
    }
    lookAt->set_range(CPLAtof(pszLookatRange));
    if( pszLookatAltitudeMode != NULL )
    {
        int isGX = FALSE;
        const int iAltitudeMode =
            kmlAltitudeModeFromString(pszLookatAltitudeMode, isGX);
        if( iAltitudeMode != kmldom::ALTITUDEMODE_CLAMPTOGROUND &&
            pszLookatAltitude == NULL )
        {
            CPLError(CE_Warning, CPLE_AppDefined,
                     "Lookat altitude should be present for altitudeMode = %s",
                     pszLookatAltitudeMode);
        }
        else if( isGX )
        {
            lookAt->set_gx_altitudemode(iAltitudeMode);
        }
        else
        {
            lookAt->set_altitudemode(iAltitudeMode);
        }
    }

    m_poKmlLayer->set_abstractview(lookAt);
}
示例#4
0
void OGRLIBKMLLayer::SetScreenOverlay(const char* pszSOHref,
                                      const char* pszSOName,
                                      const char* pszSODescription,
                                      const char* pszSOOverlayX,
                                      const char* pszSOOverlayY,
                                      const char* pszSOOverlayXUnits,
                                      const char* pszSOOverlayYUnits,
                                      const char* pszSOScreenX,
                                      const char* pszSOScreenY,
                                      const char* pszSOScreenXUnits,
                                      const char* pszSOScreenYUnits,
                                      const char* pszSOSizeX,
                                      const char* pszSOSizeY,
                                      const char* pszSOSizeXUnits,
                                      const char* pszSOSizeYUnits)
{
    KmlFactory *poKmlFactory = m_poOgrDS->GetKmlFactory (  );
    ScreenOverlayPtr so = poKmlFactory->CreateScreenOverlay();

    if( pszSOName != NULL )
        so->set_name(pszSOName);
    if( pszSODescription != NULL )
        so->set_description(pszSODescription);

    IconPtr icon = poKmlFactory->CreateIcon();
    icon->set_href(pszSOHref);
    so->set_icon(icon);

    if( pszSOOverlayX != NULL && pszSOOverlayY != NULL )
    {
        kmldom::OverlayXYPtr overlayxy = poKmlFactory->CreateOverlayXY();
        LIBKMLSetVec2(overlayxy, pszSOOverlayX, pszSOOverlayY,
                      pszSOOverlayXUnits, pszSOOverlayYUnits);
        so->set_overlayxy(overlayxy);
    }

    if( pszSOScreenX != NULL && pszSOScreenY != NULL )
    {
        kmldom::ScreenXYPtr screenxy = poKmlFactory->CreateScreenXY();
        LIBKMLSetVec2(screenxy, pszSOScreenX, pszSOScreenY,
                      pszSOScreenXUnits, pszSOScreenYUnits);
        so->set_screenxy(screenxy);
    }
    else
    {
        kmldom::ScreenXYPtr screenxy = poKmlFactory->CreateScreenXY();
        LIBKMLSetVec2(screenxy, "0.05", "0.05", NULL, NULL);
        so->set_screenxy(screenxy);
    }

    if( pszSOSizeX != NULL && pszSOSizeY != NULL )
    {
        kmldom::SizePtr sizexy = poKmlFactory->CreateSize();
        LIBKMLSetVec2(sizexy, pszSOSizeX, pszSOSizeY,
                      pszSOSizeXUnits, pszSOSizeYUnits);
        so->set_size(sizexy);
    }

    m_poKmlLayer->add_feature(so);
}
示例#5
0
void OGRLIBKMLLayer::SetCamera( const char* pszCameraLongitude,
                                const char* pszCameraLatitude,
                                const char* pszCameraAltitude,
                                const char* pszCameraHeading,
                                const char* pszCameraTilt,
                                const char* pszCameraRoll,
                                const char* pszCameraAltitudeMode )
{
    int isGX = FALSE;
    int iAltitudeMode = kmlAltitudeModeFromString(pszCameraAltitudeMode, isGX);
    if( isGX == FALSE && iAltitudeMode == kmldom::ALTITUDEMODE_CLAMPTOGROUND )
    {
        CPLError(CE_Warning, CPLE_AppDefined,
                 "Camera altitudeMode should be different from %s",
                 pszCameraAltitudeMode);
        return;
    }
    KmlFactory *poKmlFactory = m_poOgrDS->GetKmlFactory();
    CameraPtr camera = poKmlFactory->CreateCamera();
    camera->set_latitude(CPLAtof(pszCameraLatitude));
    camera->set_longitude(CPLAtof(pszCameraLongitude));
    camera->set_altitude(CPLAtof(pszCameraAltitude));
    if( pszCameraHeading != NULL )
        camera->set_heading(CPLAtof(pszCameraHeading));

    if( pszCameraTilt != NULL )
    {
        double dfTilt = CPLAtof(pszCameraTilt);
        if( dfTilt >= 0 && dfTilt <= 90 )
            camera->set_tilt(dfTilt);
        else
            CPLError(CE_Warning, CPLE_AppDefined, "Invalid value for tilt: %s",
                     pszCameraTilt);
    }

    if( pszCameraRoll != NULL )
        camera->set_roll(CPLAtof(pszCameraRoll));
    if( isGX )
        camera->set_gx_altitudemode(iAltitudeMode);
    else
        camera->set_altitudemode(iAltitudeMode);

    m_poKmlLayer->set_abstractview(camera);
}
OGRErr OGRLIBKMLLayer::CreateField (
    OGRFieldDefn * poField,
    int bApproxOK )
{

    if ( !bUpdate )
        return OGRERR_UNSUPPORTED_OPERATION;

    if( m_bUseSimpleField )
    {
        SimpleFieldPtr poKmlSimpleField = NULL;

        if ( (poKmlSimpleField =
            FieldDef2kml ( poField, m_poOgrDS->GetKmlFactory (  ) )) != NULL )
        {
            if( m_poKmlSchema == NULL )
            {
                /***** create a new schema *****/

                KmlFactory *poKmlFactory = m_poOgrDS->GetKmlFactory (  );

                m_poKmlSchema = poKmlFactory->CreateSchema (  );

                /***** set the id on the new schema *****/

                std::string oKmlSchemaID = OGRLIBKMLGetSanitizedNCName(m_pszName);
                oKmlSchemaID.append ( ".schema" );
                m_poKmlSchema->set_id ( oKmlSchemaID );
            }

            m_poKmlSchema->add_simplefield ( poKmlSimpleField );
        }
    }

    m_poOgrFeatureDefn->AddFieldDefn ( poField );

    /***** mark the layer as updated *****/

    bUpdated = TRUE;
    m_poOgrDS->Updated (  );

    return OGRERR_NONE;
}
示例#7
0
OGRErr OGRLIBKMLLayer::DeleteFeature( GIntBig nFIDIn )
{
    if( !bUpdate || !m_poKmlUpdate )
        return OGRERR_UNSUPPORTED_OPERATION;

    KmlFactory *poKmlFactory = m_poOgrDS->GetKmlFactory();
    DeletePtr poDelete = poKmlFactory->CreateDelete();
    m_poKmlUpdate->add_updateoperation(poDelete);
    PlacemarkPtr poKmlPlacemark = poKmlFactory->CreatePlacemark();
    poDelete->add_feature(poKmlPlacemark);

    const char* pszId = CPLSPrintf("%s." CPL_FRMT_GIB,
                    OGRLIBKMLGetSanitizedNCName(GetName()).c_str(), nFIDIn);
    poKmlPlacemark->set_targetid(pszId);

    /***** mark as updated *****/
    m_poOgrDS->Updated();

    return OGRERR_NONE;
}
示例#8
0
void OGRLIBKMLLayer::Finalize(DocumentPtr poKmlDocument)
{
    KmlFactory *poKmlFactory = m_poOgrDS->GetKmlFactory (  );

    if( m_bWriteRegion && m_dfRegionMinX < m_dfRegionMaxX )
    {
        RegionPtr region = poKmlFactory->CreateRegion();

        LatLonAltBoxPtr box = poKmlFactory->CreateLatLonAltBox();
        box->set_west(m_dfRegionMinX);
        box->set_east(m_dfRegionMaxX);
        box->set_south(m_dfRegionMinY);
        box->set_north(m_dfRegionMaxY);
        region->set_latlonaltbox(box);

        LodPtr lod = poKmlFactory->CreateLod();
        lod->set_minlodpixels(m_dfRegionMinLodPixels);
        lod->set_maxlodpixels(m_dfRegionMaxLodPixels);
        if( (m_dfRegionMinFadeExtent != 0 || m_dfRegionMaxFadeExtent != 0) &&
            m_dfRegionMinFadeExtent + m_dfRegionMaxFadeExtent <
                m_dfRegionMaxLodPixels - m_dfRegionMinLodPixels )
        {
            lod->set_minfadeextent(m_dfRegionMinFadeExtent);
            lod->set_maxfadeextent(m_dfRegionMaxFadeExtent);
        }

        region->set_lod(lod);
        m_poKmlLayer->set_region(region);
    }

    createkmlliststyle (poKmlFactory,
                        GetName(),
                        m_poKmlLayer,
                        poKmlDocument,
                        osListStyleType,
                        osListStyleIconHref);
}
示例#9
0
OGRLIBKMLLayer::OGRLIBKMLLayer ( const char *pszLayerName,
                                 OGRSpatialReference * poSpatialRef,
                                 OGRwkbGeometryType eGType,
                                 OGRLIBKMLDataSource * poOgrDS,
                                 ElementPtr poKmlRoot,
                                 ContainerPtr poKmlContainer,
                                 const char *pszFileName,
                                 int bNew,
                                 int bUpdate )
{

    m_poStyleTable = NULL;
    iFeature = 0;
    nFeatures = 0;
    nFID = 1;

    this->bUpdate = bUpdate;
    m_pszName = CPLStrdup ( pszLayerName );
    m_pszFileName = CPLStrdup ( pszFileName );
    m_poOgrDS = poOgrDS;

    m_poOgrSRS = new OGRSpatialReference ( NULL );
    m_poOgrSRS->SetWellKnownGeogCS ( "WGS84" );

    m_poOgrFeatureDefn = new OGRFeatureDefn ( pszLayerName );
    m_poOgrFeatureDefn->Reference (  );
    m_poOgrFeatureDefn->SetGeomType ( eGType );

    /***** store the root element pointer *****/

    m_poKmlLayerRoot = poKmlRoot;
    
    /***** store the layers container *****/

    m_poKmlLayer = poKmlContainer;

    /***** was the layer created from a DS::Open *****/

    if ( !bNew ) {

        /***** get the number of features on the layer *****/

        nFeatures = m_poKmlLayer->get_feature_array_size (  );

        /***** add the name and desc fields *****/

        const char *namefield =
            CPLGetConfigOption ( "LIBKML_NAME_FIELD", "Name" );
        const char *descfield =
            CPLGetConfigOption ( "LIBKML_DESCRIPTION_FIELD", "description" );
        const char *tsfield =
            CPLGetConfigOption ( "LIBKML_TIMESTAMP_FIELD", "timestamp" );
        const char *beginfield =
            CPLGetConfigOption ( "LIBKML_BEGIN_FIELD", "begin" );
        const char *endfield =
            CPLGetConfigOption ( "LIBKML_END_FIELD", "end" );
        const char *altitudeModefield =
            CPLGetConfigOption ( "LIBKML_ALTITUDEMODE_FIELD", "altitudeMode" );
        const char *tessellatefield =
            CPLGetConfigOption ( "LIBKML_TESSELLATE_FIELD", "tessellate" );
        const char *extrudefield =
            CPLGetConfigOption ( "LIBKML_EXTRUDE_FIELD", "extrude" );
        const char *visibilityfield =
            CPLGetConfigOption ( "LIBKML_VISIBILITY_FIELD", "visibility" );

        OGRFieldDefn oOgrFieldName (
    namefield,
    OFTString );

        m_poOgrFeatureDefn->AddFieldDefn ( &oOgrFieldName );

        OGRFieldDefn oOgrFieldDesc (
    descfield,
    OFTString );

        m_poOgrFeatureDefn->AddFieldDefn ( &oOgrFieldDesc );

        OGRFieldDefn oOgrFieldTs (
    tsfield,
    OFTDateTime );

        m_poOgrFeatureDefn->AddFieldDefn ( &oOgrFieldTs );

        OGRFieldDefn oOgrFieldBegin (
    beginfield,
    OFTDateTime );

        m_poOgrFeatureDefn->AddFieldDefn ( &oOgrFieldBegin );

        OGRFieldDefn oOgrFieldEnd (
    endfield,
    OFTDateTime );

        m_poOgrFeatureDefn->AddFieldDefn ( &oOgrFieldEnd );

        OGRFieldDefn oOgrFieldAltitudeMode (
    altitudeModefield,
    OFTString );

        m_poOgrFeatureDefn->AddFieldDefn ( &oOgrFieldAltitudeMode );

        OGRFieldDefn oOgrFieldTessellate (
    tessellatefield,
    OFTInteger );

        m_poOgrFeatureDefn->AddFieldDefn ( &oOgrFieldTessellate );

        OGRFieldDefn oOgrFieldExtrude (
    extrudefield,
    OFTInteger );

        m_poOgrFeatureDefn->AddFieldDefn ( &oOgrFieldExtrude );

        OGRFieldDefn oOgrFieldVisibility (
    visibilityfield,
    OFTInteger );

        m_poOgrFeatureDefn->AddFieldDefn ( &oOgrFieldVisibility );

        /***** get the styles *****/

        if ( m_poKmlLayer->IsA ( kmldom::Type_Document ) )
            ParseStyles ( AsDocument ( m_poKmlLayer ), &m_poStyleTable );

        /***** get the schema if the layer is a Document *****/

        m_poKmlSchema = NULL;

        if ( m_poKmlLayer->IsA ( kmldom::Type_Document ) ) {
            DocumentPtr poKmlDocument = AsDocument ( m_poKmlLayer );

            if ( poKmlDocument->get_schema_array_size (  ) ) {
                m_poKmlSchema = poKmlDocument->get_schema_array_at ( 0 );
                kml2FeatureDef ( m_poKmlSchema, m_poOgrFeatureDefn );
            }
        }

        /***** the schema is somewhere else *****/

        if (m_poKmlSchema == NULL) {

            /***** try to find the correct schema *****/

            FeaturePtr poKmlFeature;

            /***** find the first placemark *****/

            do {
                if ( iFeature >= nFeatures )
                    break;

                poKmlFeature =
                    m_poKmlLayer->get_feature_array_at ( iFeature++ );

            } while ( poKmlFeature->Type (  ) != kmldom::Type_Placemark );

            if ( iFeature <= nFeatures && poKmlFeature &&
                 poKmlFeature->Type (  ) == kmldom::Type_Placemark &&
                 poKmlFeature->has_extendeddata (  ) ) {

                ExtendedDataPtr poKmlExtendedData = poKmlFeature->
                    get_extendeddata (  );

                if ( poKmlExtendedData->get_schemadata_array_size (  ) > 0 ) {
                    SchemaDataPtr poKmlSchemaData = poKmlExtendedData->
                        get_schemadata_array_at ( 0 );

                    if ( poKmlSchemaData->has_schemaurl (  ) ) {

                        std::string oKmlSchemaUrl = poKmlSchemaData->
                            get_schemaurl (  );
                        if ( ( m_poKmlSchema =
                               m_poOgrDS->FindSchema ( oKmlSchemaUrl.
                                                       c_str (  ) ) ) ) {
                            kml2FeatureDef ( m_poKmlSchema,
                                             m_poOgrFeatureDefn );
                        }
                    }
                }
                else if ( poKmlExtendedData->get_data_array_size() > 0 )
                {
                    /* Use the <Data> of the first placemark to build the feature definition */
                    /* If others have different fields, too bad... */
                    int bLaunderFieldNames =
                        CSLTestBoolean(CPLGetConfigOption("LIBKML_LAUNDER_FIELD_NAMES", "YES"));
                    size_t nDataArraySize = poKmlExtendedData->get_data_array_size();
                    for(size_t i=0; i < nDataArraySize; i++)
                    {
                        const DataPtr& data = poKmlExtendedData->get_data_array_at(i);
                        if (data->has_name())
                        {
                            CPLString osName = data->get_name();
                            if (bLaunderFieldNames)
                                osName = LaunderFieldNames(osName);
                            OGRFieldDefn oOgrField ( osName,
                                                    OFTString );
                            m_poOgrFeatureDefn->AddFieldDefn ( &oOgrField );
                        }
                    }
                }
            }

            iFeature = 0;

        }



        /***** check if any features are another layer *****/

        m_poOgrDS->ParseLayers ( m_poKmlLayer, poSpatialRef );

    }

    /***** it was from a DS::CreateLayer *****/

    else {

        /***** mark the layer as updated *****/

        bUpdated = TRUE;

        /***** create a new schema *****/

        KmlFactory *poKmlFactory = m_poOgrDS->GetKmlFactory (  );

        m_poKmlSchema = poKmlFactory->CreateSchema (  );

        /***** set the id on the new schema *****/

        std::string oKmlSchemaID = m_pszName;
        oKmlSchemaID.append ( ".schema" );
        m_poKmlSchema->set_id ( oKmlSchemaID );
    }




}
示例#10
0
OGRErr OGRLIBKMLLayer::ICreateFeature (
    OGRFeature * poOgrFeat )
{

    if ( !bUpdate )
        return OGRERR_UNSUPPORTED_OPERATION;

    if( m_bRegionBoundsAuto && poOgrFeat->GetGeometryRef() != NULL &&
        !(poOgrFeat->GetGeometryRef()->IsEmpty()) )
    {
        OGREnvelope sEnvelope;
        poOgrFeat->GetGeometryRef()->getEnvelope(&sEnvelope);
        m_dfRegionMinX = MIN(m_dfRegionMinX, sEnvelope.MinX);
        m_dfRegionMinY = MIN(m_dfRegionMinY, sEnvelope.MinY);
        m_dfRegionMaxX = MAX(m_dfRegionMaxX, sEnvelope.MaxX);
        m_dfRegionMaxY = MAX(m_dfRegionMaxY, sEnvelope.MaxY);
    }

    FeaturePtr poKmlFeature =
        feat2kml ( m_poOgrDS, this, poOgrFeat, m_poOgrDS->GetKmlFactory (  ),
                   m_bUseSimpleField );

    if( m_poKmlLayer != NULL )
        m_poKmlLayer->add_feature ( poKmlFeature );
    else
    {
        CPLAssert( m_poKmlUpdate != NULL );
        KmlFactory *poKmlFactory = m_poOgrDS->GetKmlFactory (  );
        CreatePtr poCreate = poKmlFactory->CreateCreate();
        ContainerPtr poContainer;
        if( m_bUpdateIsFolder )
            poContainer = poKmlFactory->CreateFolder();
        else
            poContainer = poKmlFactory->CreateDocument();
        poContainer->set_targetid(OGRLIBKMLGetSanitizedNCName(GetName()));
        poContainer->add_feature ( poKmlFeature );
        poCreate->add_container(poContainer);
        m_poKmlUpdate->add_updateoperation(poCreate);
    }

    /***** update the layer class count of features  *****/

    if( m_poKmlLayer != NULL )
    {
        nFeatures++;

        const char* pszId = CPLSPrintf("%s.%d",
                        OGRLIBKMLGetSanitizedNCName(GetName()).c_str(), nFeatures);
        poOgrFeat->SetFID(nFeatures);
        poKmlFeature->set_id(pszId);
    }
    else
    {
        if( poOgrFeat->GetFID() < 0 )
        {
            static int bAlreadyWarned = FALSE;
            if( !bAlreadyWarned )
            {
                bAlreadyWarned = TRUE;
                CPLError(CE_Warning, CPLE_AppDefined,
                         "It is recommended to define a FID when calling CreateFeature() in a update document");
            }
        }
        else
        {
            const char* pszId = CPLSPrintf("%s." CPL_FRMT_GIB,
                    OGRLIBKMLGetSanitizedNCName(GetName()).c_str(), poOgrFeat->GetFID());
            poOgrFeat->SetFID(nFeatures);
            poKmlFeature->set_id(pszId);
        }
    }

    /***** mark the layer as updated *****/

    bUpdated = TRUE;
    m_poOgrDS->Updated (  );

    return OGRERR_NONE;
}