📄 qgsgpxprovider.cpp
字号:
* Return the feature type */long QgsGPXProvider::featureCount() const{ if (mFeatureType == WaypointType) return data->getNumberOfWaypoints(); if (mFeatureType == RouteType) return data->getNumberOfRoutes(); if (mFeatureType == TrackType) return data->getNumberOfTracks(); return 0;}/** * Return the number of fields */uint QgsGPXProvider::fieldCount() const{ return attributeFields.size();}const QgsFieldMap& QgsGPXProvider::fields() const{ return attributeFields;}void QgsGPXProvider::reset(){ if (mFeatureType == WaypointType) mWptIter = data->waypointsBegin(); else if (mFeatureType == RouteType) mRteIter = data->routesBegin(); else if (mFeatureType == TrackType) mTrkIter = data->tracksBegin();}bool QgsGPXProvider::isValid(){ return mValid;}bool QgsGPXProvider::addFeatures(QgsFeatureList & flist){ // add all the features for (QgsFeatureList::iterator iter = flist.begin(); iter != flist.end(); ++iter) { if (!addFeature(*iter)) return false; } // write back to file QFile file(mFileName); if (!file.open(QIODevice::WriteOnly)) return false; QTextStream ostr(&file); data->writeXML(ostr); return true;}bool QgsGPXProvider::addFeature(QgsFeature& f){ unsigned char* geo = f.geometry()->wkbBuffer(); QGis::WKBTYPE wkbType = f.geometry()->wkbType(); bool success = false; GPSObject* obj = NULL; const QgsAttributeMap& attrs(f.attributeMap()); QgsAttributeMap::const_iterator it; // is it a waypoint? if (mFeatureType == WaypointType && geo != NULL && wkbType == QGis::WKBPoint) { // add geometry Waypoint wpt; std::memcpy(&wpt.lon, geo+5, sizeof(double)); std::memcpy(&wpt.lat, geo+13, sizeof(double)); // add waypoint-specific attributes for (it = attrs.begin(); it != attrs.end(); ++it) { if (it.key() == EleAttr) { bool eleIsOK; double ele = it->toDouble(&eleIsOK); if (eleIsOK) wpt.ele = ele; } else if (it.key() == SymAttr) { wpt.sym = it->toString(); } } GPSData::WaypointIterator iter = data->addWaypoint(wpt); success = true; obj = &(*iter); } // is it a route? if (mFeatureType == RouteType && geo != NULL && wkbType == QGis::WKBLineString) { Route rte; // reset bounds rte.xMin = std::numeric_limits<double>::max(); rte.xMax = -std::numeric_limits<double>::max(); rte.yMin = std::numeric_limits<double>::max(); rte.yMax = -std::numeric_limits<double>::max(); // add geometry int nPoints; std::memcpy(&nPoints, geo + 5, 4); for (int i = 0; i < nPoints; ++i) { double lat, lon; std::memcpy(&lon, geo + 9 + 16 * i, sizeof(double)); std::memcpy(&lat, geo + 9 + 16 * i + 8, sizeof(double)); Routepoint rtept; rtept.lat = lat; rtept.lon = lon; rte.points.push_back(rtept); rte.xMin = rte.xMin < lon ? rte.xMin : lon; rte.xMax = rte.xMax > lon ? rte.xMax : lon; rte.yMin = rte.yMin < lat ? rte.yMin : lat; rte.yMax = rte.yMax > lat ? rte.yMax : lat; } // add route-specific attributes for (it = attrs.begin(); it != attrs.end(); ++it) { if (it.key() == NumAttr) { bool numIsOK; long num = it->toInt(&numIsOK); if (numIsOK) rte.number = num; } } GPSData::RouteIterator iter = data->addRoute(rte); success = true; obj = &(*iter); } // is it a track? if (mFeatureType == TrackType && geo != NULL && wkbType == QGis::WKBLineString) { Track trk; TrackSegment trkseg; // reset bounds trk.xMin = std::numeric_limits<double>::max(); trk.xMax = -std::numeric_limits<double>::max(); trk.yMin = std::numeric_limits<double>::max(); trk.yMax = -std::numeric_limits<double>::max(); // add geometry int nPoints; std::memcpy(&nPoints, geo + 5, 4); for (int i = 0; i < nPoints; ++i) { double lat, lon; std::memcpy(&lon, geo + 9 + 16 * i, sizeof(double)); std::memcpy(&lat, geo + 9 + 16 * i + 8, sizeof(double)); Trackpoint trkpt; trkpt.lat = lat; trkpt.lon = lon; trkseg.points.push_back(trkpt); trk.xMin = trk.xMin < lon ? trk.xMin : lon; trk.xMax = trk.xMax > lon ? trk.xMax : lon; trk.yMin = trk.yMin < lat ? trk.yMin : lat; trk.yMax = trk.yMax > lat ? trk.yMax : lat; } // add track-specific attributes for (it = attrs.begin(); it != attrs.end(); ++it) { if (it.key() == NumAttr) { bool numIsOK; long num = it->toInt(&numIsOK); if (numIsOK) trk.number = num; } } trk.segments.push_back(trkseg); GPSData::TrackIterator iter = data->addTrack(trk); success = true; obj = &(*iter); } // add common attributes if (obj) { for (it = attrs.begin(); it != attrs.end(); ++it) { if (it.key() == NameAttr) { obj->name = it->toString(); } else if (it.key() == CmtAttr) { obj->cmt = it->toString(); } else if (it.key() ==DscAttr) { obj->desc = it->toString(); } else if (it.key() == SrcAttr) { obj->src = it->toString(); } else if (it.key() == URLAttr) { obj->url = it->toString(); } else if (it.key() == URLNameAttr) { obj->urlname = it->toString(); } } } return success;}bool QgsGPXProvider::deleteFeatures(const QgsFeatureIds & id){ if (mFeatureType == WaypointType) data->removeWaypoints(id); else if (mFeatureType == RouteType) data->removeRoutes(id); else if (mFeatureType == TrackType) data->removeTracks(id); // write back to file QFile file(mFileName); if (!file.open(QIODevice::WriteOnly)) return false; QTextStream ostr(&file); data->writeXML(ostr); return true;}bool QgsGPXProvider::changeAttributeValues(const QgsChangedAttributesMap & attr_map){ QgsChangedAttributesMap::const_iterator aIter = attr_map.begin(); if (mFeatureType == WaypointType) { GPSData::WaypointIterator wIter = data->waypointsBegin(); for (; wIter != data->waypointsEnd() && aIter != attr_map.end(); ++wIter) { if (wIter->id == aIter.key()) { changeAttributeValues(*wIter, aIter.value()); ++aIter; } } } else if (mFeatureType == RouteType) { GPSData::RouteIterator rIter = data->routesBegin(); for (; rIter != data->routesEnd() && aIter != attr_map.end(); ++rIter) { if (rIter->id == aIter.key()) { changeAttributeValues(*rIter, aIter.value()); ++aIter; } } } if (mFeatureType == TrackType) { GPSData::TrackIterator tIter = data->tracksBegin(); for (; tIter != data->tracksEnd() && aIter != attr_map.end(); ++tIter) { if (tIter->id == aIter.key()) { changeAttributeValues(*tIter, aIter.value()); ++aIter; } } } // write back to file QFile file(mFileName); if (!file.open(QIODevice::WriteOnly)) return false; QTextStream ostr(&file); data->writeXML(ostr); return true;}void QgsGPXProvider::changeAttributeValues(GPSObject& obj, const QgsAttributeMap& attrs){ QgsAttributeMap::const_iterator aIter; // TODO: if (attrs.contains(NameAttr)) obj.name = attrs[NameAttr].toString(); if (attrs.contains(CmtAttr)) obj.cmt = attrs[CmtAttr].toString(); if (attrs.contains(DscAttr)) obj.desc = attrs[DscAttr].toString(); if (attrs.contains(SrcAttr)) obj.src = attrs[SrcAttr].toString(); if (attrs.contains(URLAttr)) obj.url = attrs[URLAttr].toString(); if (attrs.contains(URLNameAttr)) obj.urlname = attrs[URLNameAttr].toString(); // waypoint-specific attributes Waypoint* wpt = dynamic_cast<Waypoint*>(&obj); if (wpt != NULL) { if (attrs.contains(SymAttr)) wpt->sym = attrs[SymAttr].toString(); if (attrs.contains(EleAttr)) { bool eleIsOK; double ele = attrs[EleAttr].toDouble(&eleIsOK); if (eleIsOK) wpt->ele = ele; } } // route- and track-specific attributes GPSExtended* ext = dynamic_cast<GPSExtended*>(&obj); if (ext != NULL) { if (attrs.contains(NumAttr)) { bool eleIsOK; double ele = attrs[NumAttr].toDouble(&eleIsOK); if (eleIsOK) wpt->ele = ele; } }}QVariant QgsGPXProvider::getDefaultValue(int fieldId){ if (fieldId == SrcAttr) return tr("Digitized in QGIS"); return QVariant();}/** * Check to see if the point is within the selection rectangle */bool QgsGPXProvider::boundsCheck(double x, double y){ bool inBounds = (((x <= mSelectionRectangle->xMax()) && (x >= mSelectionRectangle->xMin())) && ((y <= mSelectionRectangle->yMax()) && (y >= mSelectionRectangle->yMin()))); QString hit = inBounds?"true":"false"; return inBounds;}QString QgsGPXProvider::name() const{ return GPX_KEY;} // QgsGPXProvider::name()QString QgsGPXProvider::description() const{ return GPX_DESCRIPTION;} // QgsGPXProvider::description()QgsSpatialRefSys QgsGPXProvider::getSRS(){ return QgsSpatialRefSys(); // use default SRS - it's WGS84}/** * Class factory to return a pointer to a newly created * QgsGPXProvider object */QGISEXTERN QgsGPXProvider * classFactory(const QString *uri) { return new QgsGPXProvider(*uri);}/** Required key function (used to map the plugin to a data store type)*/QGISEXTERN QString providerKey(){ return GPX_KEY;}/** * Required description function */QGISEXTERN QString description(){ return GPX_DESCRIPTION;} /** * Required isProvider function. Used to determine if this shared library * is a data provider plugin */QGISEXTERN bool isProvider(){ return true;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -