def makeShapeFiles(self): myFileName = QgsProject.instance().fileName() myFileName = str(myFileName) # if this is a windows OS then the filename needs to have '/' converted to '\' if myFileName.find('windows') != 0 or myFileName.find('Windows') != 0: myFileName = os.path.normpath(myFileName) myFilePath = os.path.dirname(myFileName) myShapeFileNamesDlg = ShapeFileNames(myFilePath) isOK = myShapeFileNamesDlg.exec_() CRS = QgsCoordinateReferenceSystem() crsIsOk = CRS.createFromOgcWmsCrs("EPSG:%d" % self.GPSTracks[0][0].datumEPSG) if not crsIsOk: QMessageBox.warning(self.iface.mainWindow(),"trackGPS - makeShapeFiles","Error creating CRS from"+\ " EPSG ID:" + str(self.GPSTracks[0][0].datumEPSG)) if isOK and crsIsOk: if len(myShapeFileNamesDlg.lnePointsFileName.text()) > 0: # set up the fields for the Points SHAPE file box = QgsFields() box.append(QgsField("LATITUDE", QVariant.Double , "Real", 9, 6)) box.append(QgsField("LONGITUDE", QVariant.Double, "Real", 10, 6)) box.append(QgsField("NUMOFSATS", QVariant.Int, "Integer", 2, 0)) box.append(QgsField("HDOP", QVariant.Double, "Real", 4, 2)) box.append(QgsField("DATETIME", QVariant.String, "String", 19, 0)) box.append(QgsField("FIXTYPE", QVariant.String, "String", 1, 0)) box.append(QgsField("BEARING", QVariant.Double, "Real", 6, 2)) box.append(QgsField("SPEED-KPH", QVariant.Double, "Real", 5, 1)) box.append(QgsField("TRACKNUM", QVariant.Int, "Integer", 2, 0)) # set up the CRS # open the points SHAPE file pointsFile, fileOK = self.createSHAPEfile(myShapeFileNamesDlg.lnePointsFileName.text(),box,QGis.WKBPoint,CRS) if fileOK: for j in range(len(self.GPSTracks)): for i in range(len(self.GPSTracks[j])): theFeature = QgsFeature() latitude = self.GPSTracks[j][i].latitude if self.GPSTracks[j][i].latitudeNS == 'S': latitude = -latitude longitude = self.GPSTracks[j][i].longitude if self.GPSTracks[j][i].longitudeEW == 'W': longitude = -longitude theFeature.setGeometry(QgsGeometry.fromPoint(QgsPoint(longitude,latitude))) theFeature.setAttributes([0, latitude]) theFeature.setAttributes([1, longitude]) theFeature.setAttributes([2, self.GPSTracks[j][i].numSatellites]) theFeature.setAttributes([3, self.GPSTracks[j][i].hdop]) theFeature.setAttributes([4, self.GPSTracks[j][i].theDateTime]) theFeature.setAttributes([5, self.GPSTracks[j][i].fixQuality]) theFeature.setAttributes([6, self.GPSTracks[j][i].bearing]) theFeature.setAttributes([7, self.GPSTracks[j][i].speed]) theFeature.setAttributes([8, j+1]) pointsFile.addFeature(theFeature) del pointsFile # del file object to force a flush and close if len(myShapeFileNamesDlg.lneLinesFileName.text()) > 0: # set up the fields for the Lines SHAPE file lines_box = QgsFields() lines_box.append(QgsField("SDATETIME", QVariant.String, "String", 19, 0)) lines_box.append(QgsField("EDATETIME", QVariant.String, "String", 19, 0)) lines_box.append(QgsField("TRACKNUM", QVariant.Int, "Integer", 2, 0)) linesFile, fileOK = self.createSHAPEfile(myShapeFileNamesDlg.lneLinesFileName.text(),lines_box,QGis.WKBLineString,CRS) if fileOK: for j in range(len(self.GPSTracks)): theFeature = QgsFeature() pointsList = [] for i in range(len(self.GPSTracks[j])): latitude = self.GPSTracks[j][i].latitude if self.GPSTracks[j][i].latitudeNS == 'S': latitude = -latitude longitude = self.GPSTracks[j][i].longitude if self.GPSTracks[j][i].longitudeEW == 'W': longitude = -longitude pointsList.append(QgsPoint(longitude,latitude)) theFeature.setGeometry(QgsGeometry.fromPolyline(pointsList)) theFeature.setAttributes([0, self.GPSTracks[j][0].theDateTime]) theFeature.setAttributes([1, self.GPSTracks[j][len(self.GPSTracks[j])-1].theDateTime]) theFeature.setAttributes([2, j+1]) linesFile.addFeature(theFeature) del linesFile # del file object to force a flush and close
def makeShapeFiles(self): """ Function to save data to SHAPE files """ myFileName = QgsProject.instance().fileName() myFileName = str(myFileName) # if this is a windows OS then the filename needs to have '/' converted to '\' if myFileName.find('windows') != 0 or myFileName.find('Windows') != 0: myFileName = os.path.normpath(myFileName) myFilePath = os.path.dirname(myFileName) #mymessage = 'QGIS Project Pathname=\'' + myFilePath + '\'' #QMessageBox.information(self.iface.mainWindow(),"trackGps",mymessage,QMessageBox.Ok) myShapeFileNamesDlg = ShapeFileNames(myFilePath) isOK = myShapeFileNamesDlg.exec_() CRS = QgsCoordinateReferenceSystem() crsIsOk = CRS.createFromEpsg(self.GPSTracks[0][0].datumEPSG) if not crsIsOk: QMessageBox.warning(self.iface.mainWindow(),"trackGPS - makeShapeFiles","Error creating CRS from"+\ " EPSG ID:" + str(self.GPSTracks[0][0].datumEPSG)) if isOK and crsIsOk: if len(myShapeFileNamesDlg.lnePointsFileName.text()) > 0: # set up the fields for the Points SHAPE file latf = QgsField ("LATITUDE", QVariant.Double, "Real", 9, 6) lonf = QgsField ("LONGITUDE", QVariant.Double, "Real", 10, 6) nums = QgsField ("NUMOFSATS", QVariant.Int, "Integer", 2, 0) hdop = QgsField ("HDOP", QVariant.Double, "Real", 4, 2) theDateTime = QgsField ("DATETIME", QVariant.String, "String", 19, 0) fixType = QgsField ("FIXTYPE", QVariant.String, "String", 1, 0) #fixType = QgsField ("FIXTYPE", QVariant.String) #fixType.setLength(1) bearing = QgsField ("BEARING", QVariant.Double, "Real", 6, 2) speed = QgsField ("SPEED-KPH", QVariant.Double, "Real", 5, 1) trackNumber = QgsField ("TRACKNUM", QVariant.Int, "Integer", 2, 0) qFields = {} qFields[0] = latf qFields[1] = lonf qFields[2] = nums qFields[3] = hdop qFields[4] = theDateTime qFields[5] = fixType qFields[6] = bearing qFields[7] = speed qFields[8] = trackNumber # set up the CRS # open the points SHAPE file # pointsFile = QgsVectorFileWriter(myShapeFileNamesDlg.lnePointsFileName.text(),"System",qFields,QGis.WKBPoint,CRS) pointsFile, fileOK = self.createSHAPEfile(myShapeFileNamesDlg.lnePointsFileName.text(),qFields,QGis.WKBPoint,CRS) if fileOK: for j in range(len(self.GPSTracks)): for i in range(len(self.GPSTracks[j])): theFeature = QgsFeature() latitude = self.GPSTracks[j][i].latitude if self.GPSTracks[j][i].latitudeNS == 'S': latitude = -latitude longitude = self.GPSTracks[j][i].longitude if self.GPSTracks[j][i].longitudeEW == 'W': longitude = -longitude theFeature.setGeometry(QgsGeometry.fromPoint(QgsPoint(longitude,latitude))) theFeature.addAttribute(0, QVariant(latitude)) theFeature.addAttribute(1, QVariant(longitude)) theFeature.addAttribute(2, QVariant(self.GPSTracks[j][i].numSatellites)) theFeature.addAttribute(3, QVariant(self.GPSTracks[j][i].hdop)) theFeature.addAttribute(4, QVariant(self.GPSTracks[j][i].theDateTime)) theFeature.addAttribute(5, QVariant(self.GPSTracks[j][i].fixQuality)) theFeature.addAttribute(6, QVariant(self.GPSTracks[j][i].bearing)) theFeature.addAttribute(7, QVariant(self.GPSTracks[j][i].speed)) theFeature.addAttribute(8, QVariant(j+1)) pointsFile.addFeature(theFeature) del pointsFile # del file object to force a flush and close if len(myShapeFileNamesDlg.lneLinesFileName.text()) > 0: # set up the fields for the Lines SHAPE file startDateTime = QgsField ("SDATETIME", QVariant.String, "String", 19, 0) endDateTime = QgsField ("EDATETIME", QVariant.String, "String", 19, 0) trackNumber = QgsField ("TRACKNUM", QVariant.Int, "Integer", 2, 0) qFields = {} qFields[0] = startDateTime qFields[1] = endDateTime qFields[2] = trackNumber linesFile, fileOK = self.createSHAPEfile(myShapeFileNamesDlg.lneLinesFileName.text(),qFields,QGis.WKBLineString,CRS) if fileOK: for j in range(len(self.GPSTracks)): theFeature = QgsFeature() pointsList = [] for i in range(len(self.GPSTracks[j])): latitude = self.GPSTracks[j][i].latitude if self.GPSTracks[j][i].latitudeNS == 'S': latitude = -latitude longitude = self.GPSTracks[j][i].longitude if self.GPSTracks[j][i].longitudeEW == 'W': longitude = -longitude pointsList.append(QgsPoint(longitude,latitude)) theFeature.setGeometry(QgsGeometry.fromPolyline(pointsList)) theFeature.addAttribute(0, QVariant(self.GPSTracks[j][0].theDateTime)) theFeature.addAttribute(1, QVariant(self.GPSTracks[j][len(self.GPSTracks[j])-1].theDateTime)) theFeature.addAttribute(2, QVariant(j+1)) linesFile.addFeature(theFeature) del linesFile # del file object to force a flush and close