TypeError:无法使用geojson node.js读取未定义的属性“ hasOwnProperty”

时间:2019-02-12 12:04:45

标签: node.js mongodb express geojson

为此,我尝试通过读取文件来创建geojson,然后读取GPS数据和加速度计数据,然后合并这些数据,以尝试创建geojson并将该geojson数据存储在文件中。

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/db_smartbus";
var moment = require('moment');
var fs = require("fs");
var GeoJSON = require('geojson');
var busDate = [];
var busdata = [];
var array1 = [];
var array2 = [];

var folder_Name = process.argv[2];
var sensorName = process.argv[3];
var GPSfolder_Name = process.argv[4];
var GPSpath = process.argv[5];
var routes = process.argv[6];
var Busno = process.argv[7];


if (Busno == '702') {
Busname = 'Bus2';
}
else {
Busname = 'Bus1';
};

var Accel = Path + folder_Name + '/' + sensorName + '/';
var GPS = Path + GPSfolder_Name + '/' + GPSpath + '/';

GPS_dateFolders = fs.readdirSync(GPS);
Accel_dateFolders = fs.readdirSync(Accel);

var split_Routes = routes.split('-');
var from_Routes = split_Routes[0];
var to_Routes = split_Routes[1];

MongoClient.connect(url, function (err, db) {
if (err)
    throw err;
console.log("connected");
var dbo = db.db("db_smartbus");

dbo.collection("Route_Sensordata").find({ Bus: Busno }).toArray(function 
(err, result) {
    if (err) throw err;

    for (var bus = 0; bus < result.length; bus++) {
        if (from_Routes == result[bus].Origin && to_Routes == 
 result[bus].Destination) {
            busDate.push(result[bus].Date)
        }
        else {
        }
    }

    console.log(busDate.length);

    for (busdate_count = 0; busdate_count < busDate.length; busdate_count++) 
    {

        var regex = /:/gi;
        var replaceDate = busDate[busdate_count].replace(regex, '-')

        var date = new Date(replaceDate);//process.argv[2]
        var yyyymmdd = moment(date).format('YYYYMMDD');
        var y = date.getFullYear().toString();
        var m = (date.getMonth() + 1).toString();
        var d = (date.getDate()).toString();
        (d.length == 1) && (d = '0' + d);
        (m.length == 1) && (m = '0' + m);
        var labeledDate = y + '-' + m + '-' + d;

        for (var dateCount_Accel = 0; dateCount_Accel < Accel_dateFolders.length; dateCount_Accel++) {

            receivedir_AccelDate = fs.readdirSync(Accel + Accel_dateFolders[dateCount_Accel]);
            console.log(Accel + Accel_dateFolders[dateCount_Accel]);
            if (Accel_dateFolders[dateCount_Accel] === yyyymmdd) {

                for (var timeStampCount_Accel = 0; timeStampCount_Accel < receivedir_AccelDate.length; timeStampCount_Accel++) {
                    receivedir_AccelTimestamp = fs.readdirSync(Accel + Accel_dateFolders[dateCount_Accel] + '/' + receivedir_AccelDate[timeStampCount_Accel]);
                    console.log(Accel + Accel_dateFolders[dateCount_Accel] + '/' + receivedir_AccelDate[timeStampCount_Accel]);
                    for (var fileCount_Accel = 0; fileCount_Accel < receivedir_AccelTimestamp.length; fileCount_Accel++) {

                        if (fs.lstatSync(Accel + Accel_dateFolders[dateCount_Accel] + '/' + receivedir_AccelDate[timeStampCount_Accel] + '/' + receivedir_AccelTimestamp[fileCount_Accel]).isDirectory()) {
                        }
                        else {
                            var filenm = Accel + Accel_dateFolders[dateCount_Accel] + '/' + receivedir_AccelDate[timeStampCount_Accel] + '/' + receivedir_AccelTimestamp[fileCount_Accel];

                            fileext = filenm.split('.');

                            if (fileext[1] == 'json') {

                                allfiles_Accel = fs.readFileSync(Accel + Accel_dateFolders[dateCount_Accel] + '/' + receivedir_AccelDate[timeStampCount_Accel] + '/' + receivedir_AccelTimestamp[fileCount_Accel], 'utf8');
                                parseddata_Accel = JSON.parse(allfiles_Accel);
                                array1.push(parseddata_Accel.sensorData[0]);
                            }
                            else {

                            }
                        }

                    }
                }
            }
            else {

            }

        }

        for (var dateCount_GPS = 0; dateCount_GPS < GPS_dateFolders.length; dateCount_GPS++) {

            receivedir_GPSDate = fs.readdirSync(GPS + GPS_dateFolders[dateCount_GPS]);

            if (GPS_dateFolders[dateCount_GPS] === yyyymmdd) {

                for (var timeStampCount_GPS = 0; timeStampCount_GPS < receivedir_GPSDate.length; timeStampCount_GPS++) {
                    receivedir_GPSTimestamp = fs.readdirSync(GPS + GPS_dateFolders[dateCount_GPS] + '/' + receivedir_GPSDate[timeStampCount_GPS]);

                    for (var fileCount_GPS = 0; fileCount_GPS < receivedir_GPSTimestamp.length; fileCount_GPS++) {

                        if (fs.lstatSync(GPS + GPS_dateFolders[dateCount_GPS] + '/' + receivedir_GPSDate[timeStampCount_GPS] + '/' + receivedir_GPSTimestamp[fileCount_GPS]).isDirectory()) {

                        }
                        else {
                            allfiles_GPS = fs.readFileSync(GPS + GPS_dateFolders[dateCount_GPS] + '/' + receivedir_GPSDate[timeStampCount_GPS] + '/' + receivedir_GPSTimestamp[fileCount_GPS], 'utf8');

                            parseddata_GPS = JSON.parse(allfiles_GPS);
                            var splitRaw = (parseddata_GPS.sensorData[0].Raw).split(',');
                            var latDD = parseInt(splitRaw[3] / 100);
                            var latSS = parseFloat(splitRaw[3] - latDD * 100);
                            var finallat = latDD + latSS / 60;
                            var lngDD = parseInt(splitRaw[5] / 100);
                            var lngSS = parseFloat(splitRaw[5] - lngDD * 100);
                            var finallng = lngDD + lngSS / 60;
                            array2.push({ Raw: parseddata_GPS.sensorData[0].Raw, captureDateTime: parseddata_GPS.sensorData[0].captureDateTime, Latitude: -finallat, Longitude: -finallng });

                        }

                    }
                }
            }
            else {

            }

        }

        for (var merge_count = 0; merge_count < array1.length; merge_count++) {
            busdata.push({ SensordataAccel: array1[merge_count], SensordataGps: array2[merge_count] });

        }

        if (sensorName == 'Accel3' || sensorName == 'Accel2') {
            sensorName = 'Accel1';
        }
        else if (sensorName == 'Accel5' || sensorName == 'Accel4') {
            sensorName = 'Accel2';
        }
        else if (sensorName == 'Accel7' || sensorName == 'Accel6') {
            sensorName = 'Accel3';
        }
        else if (sensorName == 'Accel9' || sensorName == 'Accel8') {
            sensorName = 'Accel4';
        }

        var d = GeoJSON.parse(busdata, { Point: ['SensordataGps.Latitude', 'SensordataGps.Longitude'] });

        fs.appendFileSync(Path + '/' + 'georeferenced_rawdata' + '/' + from_Routes.toUpperCase() + '-' + to_Routes.toUpperCase() + '-' + Busname.toUpperCase() + '-' + sensorName.toUpperCase(), JSON.stringify(d), 'utf8');
        busdata = [];
        array1 = [];
        array2 = [];

    }
})

})

通过实施此解决方案,当我创建geojson并将geojson数据存储在文件中时出现此错误

0 个答案:

没有答案