- fix scelta provincia (il bottone Avanti non veniva disabilitato).
- Se non scelgo la provincia, non deve farmi vedere la App...
This commit is contained in:
@@ -10,6 +10,8 @@ const tools = require('../tools/general');
|
||||
|
||||
const { ObjectID } = require('mongodb');
|
||||
|
||||
const fs = require('fs-extra');
|
||||
|
||||
const shared_consts = require('../tools/shared_nodejs');
|
||||
|
||||
// Resolving error Unknown modifier: $pushAll
|
||||
@@ -49,6 +51,9 @@ const CitySchema = new Schema({
|
||||
type: String,
|
||||
maxlength: 3,
|
||||
},
|
||||
geojson: {
|
||||
type: Object, // Tipo che può contenere le features GeoJSON
|
||||
},
|
||||
});
|
||||
|
||||
CitySchema.pre('save', async function (next) {
|
||||
@@ -198,6 +203,73 @@ CitySchema.statics.findAllIdApp = async function (idapp) {
|
||||
return await City.find(myfind);
|
||||
};
|
||||
|
||||
CitySchema.statics.getGeoJsonByProvince = async function (prov) {
|
||||
let ris = null;
|
||||
if (prov)
|
||||
ris = await City.find({ prov }).lean();
|
||||
else
|
||||
ris = await City.find({}).lean();
|
||||
|
||||
try {
|
||||
if (ris) {
|
||||
const arrjson = ris
|
||||
.filter(record => record.geojson && typeof record.geojson === 'object' && record.geojson.geometry) // Prima filtra per mantenere solo gli oggetti con "geometry"
|
||||
.map((record, index) => {
|
||||
// Crea un nuovo oggetto con gli attributi desiderati da ogni record
|
||||
const newRecord = {
|
||||
...record.geojson, // Spread dell'oggetto geojson per prendere tutte le sue proprietà
|
||||
id: (index + 1).toString(), // Aggiunge un valore "id" incrementale in base all'indice
|
||||
};
|
||||
|
||||
// Aggiungi o aggiorna la proprietà "prov" in "properties" dentro l'oggetto geojson
|
||||
if (newRecord.properties) {
|
||||
newRecord.properties.prov = record.prov; // Se "properties" esiste già
|
||||
} else {
|
||||
newRecord.properties = { prov: record.prov }; // Crea "properties" se non esiste
|
||||
}
|
||||
|
||||
return newRecord;
|
||||
});
|
||||
|
||||
return arrjson;
|
||||
}
|
||||
return [];
|
||||
} catch (e) {
|
||||
console.error('Err', e);
|
||||
}
|
||||
|
||||
return null
|
||||
};
|
||||
|
||||
|
||||
CitySchema.statics.insertGeojsonToMongoDB = async function (nomefilejson) {
|
||||
try {
|
||||
// Lettura del file GeoJSON
|
||||
const geojson = await fs.readJson(nomefilejson); // Sostituisci con il percorso del tuo file GeoJSON
|
||||
|
||||
let inseriti = 0;
|
||||
const numcomuni = geojson.features.length;
|
||||
for (const citta of geojson.features) {
|
||||
|
||||
// Identifica il documento esistente in cui vuoi aggiungere le features
|
||||
const reccity = await City.findOne({ istat: String(citta.properties.ISTAT).padStart(6, '0') });
|
||||
|
||||
if (reccity) {
|
||||
const ris = await City.updateOne({ _id: reccity._id }, { $set: { geojson: citta } });
|
||||
if (ris.ok === 1) {
|
||||
inseriti++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
console.log(`${inseriti} su ${numcomuni} comuni inseriti.`);
|
||||
} catch (e) {
|
||||
console.error('Err', e);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
const City = mongoose.model('City', CitySchema);
|
||||
|
||||
Reference in New Issue
Block a user