How Use GeoWithin with mongodb C# Driver 2.4 - mongodb

I need to use GeoWithin to find nearly point to the user after I take his location, did any body used before?

By the way This is a sample for near and GeoWithin:
var point = GeoJson.Point(GeoJson.Geographic(-73.97, 40.77));
var filter = Builders<BsonDocument>.Filter.Near("location", point, 10);
var a = collection.Find(filter1).Any();
var filter2 = Builders<BsonDocument>.Filter.GeoWithin("location", point);
var b = collection.Find(filter2).Any();
you can also use GeoWithinPolygon and GeoWithinCenter.
double[,] polygon = new double[,] { { -73.97, 40.77 }, { -73.9928, 40.7193 }, { -73.9375, 40.8303 }, { -73.97, 40.77 } };
var filter3 = Builders<BsonDocument>.Filter.GeoWithinPolygon("location", polygon);
var c = collection.Find(filter3).Any();
var filter4 = Builders<BsonDocument>.Filter.GeoWithinCenter("location", -73.97, 40.77, 10);
var d = collection.Find(filter4).Any();
Hope it helps.

Related

Error in Google Earth Engine: Computed value is too large

I'm trying to achieve classification for image collection over 30 years as the code shows below:
var roi = ee.FeatureCollection("users/471033961/YellowRiver");
var empty = ee.Image().toByte();
var outline = empty.paint({
featureCollection:roi,
color:0,
width:3
});
Map.addLayer(outline, {palette: "ff0000"}, "outline");
//////////////////////////////landsat 5,7
function l57class(year){
var startDate = ee.Date.fromYMD(year, 1, 1);
var endDate = ee.Date.fromYMD(year+1, 1, 1);
var LC57_BANDS = ['B1', 'B2', 'B3', 'B4', 'B5', 'B7']; //Landsat57
var STD_NAMES = ['B2', 'B3', 'B4', 'B5', 'B6', 'B7'];
var cloudMaskL457 = function(image) {
var qa = image.select('pixel_qa');
// If the cloud bit (5) is set and the cloud confidence (7) is high
// or the cloud shadow bit is set (3), then it's a bad pixel.
var cloud = qa.bitwiseAnd(1 << 5)
.and(qa.bitwiseAnd(1 << 7))
.or(qa.bitwiseAnd(1 << 3));
// Remove edge pixels that don't occur in all bands
var mask2 = image.mask().reduce(ee.Reducer.min());
return image.updateMask(cloud.not()).updateMask(mask2);
}
var collection1 = ee.ImageCollection("LANDSAT/LE07/C01/T1_SR")
.filterDate(startDate, endDate)
.filterBounds(roi)
.filter(ee.Filter.lte('CLOUD_COVER',10))
.map(cloudMaskL457)
var collection2 = ee.ImageCollection("LANDSAT/LT05/C01/T1_SR")
.filterDate(startDate, endDate)
.filterBounds(roi)
.filter(ee.Filter.lte('CLOUD_COVER',10))
.map(cloudMaskL457)
var collection = collection1.merge(collection2).select(LC57_BANDS, STD_NAMES)
var image=collection.mosaic().clip(roi)
function NDWI(img){
var nir = img.select('B4');
var green = img.select('B2');
var ndwi = img.expression(
'(B3-B5)/(B3+B5)',
{
'B5':nir,
'B3':green
});
return ndwi;
}
function EWI(img){
var swir1 = img.select('B6')
var nir = img.select('B5');
var green = img.select('B3');
var ewi = img.expression(
'(B3-B5-B6)/(B3+B5+B6)',
{
'B6':swir1,
'B5':nir,
'B3':green
});
return ewi;
}
var MNDWI = image.normalizedDifference(['B3', 'B6']).rename('MNDWI');
var ndbi = image.normalizedDifference(['B6', 'B5']).rename('NDBI');
var ndvi = image.normalizedDifference(['B5', 'B4']).rename('NDVI');
var ewi = EWI(image).rename('EWI');
var ndwi = NDWI(image).rename('NDWI');
var lswi = image.normalizedDifference(['B5','B6']).rename('LSWI')
var nbr2 = image.normalizedDifference(["B6", "B7"]).rename("NBR2")
var awei = image.expression(
'4*(green-SWIR1)-(0.25*NIR+2.75*SWIR2)',{
green:image.select('B3'),
NIR:image.select('B5'),
SWIR1:image.select('B6'),
SWIR2:image.select('B7'),
}).float().rename('AWEI')
image=image
.addBands(ndvi)
.addBands(ndbi)
.addBands(MNDWI)
.addBands(ndwi)
.addBands(ewi)
.addBands(lswi)
.addBands(awei)
.addBands(nbr2)
var classNames = city.merge(water).merge(tree).merge(crop).merge(bare);
var bands = ['B2', 'B3', 'B4', 'B5', 'B6', 'B7','MNDWI','NDBI','NDVI','EWI','NDWI','AWEI','LSWI',"NBR2"];
var training = image.select(bands).sampleRegions({
collection: classNames,
properties: ['landcover'],
scale: 30
});
var withRandom = training.randomColumn('random');
var split = 0.8;
var trainingPartition = withRandom.filter(ee.Filter.lt('random', split));
var testingPartition = withRandom.filter(ee.Filter.gte('random', split));
var classProperty = 'landcover';
var classifier = ee.Classifier.smileRandomForest(100).train({
features: trainingPartition,
classProperty: 'landcover',
inputProperties: bands
});
var classified = image.select(bands).classify(classifier);
var test = testingPartition.classify(classifier);
var confusionMatrix = test.errorMatrix('landcover', 'classification');
print(year)
print('confusionMatrix',confusionMatrix);
print('overall accuracy', confusionMatrix.accuracy());
print('kappa accuracy', confusionMatrix.kappa());
Map.addLayer(classified,{}, "classified"+year);
Export.image.toDrive({
image: classified,
description: 'YR_cart'+year,
folder: 'YR_cart'+year,
scale: 30,
region: roi,
maxPixels:34e10
});
}
////////////////////////////////////////////////////////////////////////////////
function l8class(year){
var startDate = ee.Date.fromYMD(year, 1, 1);
var endDate = ee.Date.fromYMD(year+1, 1, 1);
var collection = ee.ImageCollection("LANDSAT/LC08/C01/T1_RT")
.filterDate(startDate, endDate)
.filterBounds(roi)
.filter(ee.Filter.lte('CLOUD_COVER',20))
var remove_cloud=function(collection)
{
var fun_calCloudScore = function(image) {
return ee.Algorithms.Landsat.simpleCloudScore(ee.Image(image));
};
var calCloudScore = ee.ImageCollection(collection)
.map(fun_calCloudScore)
;
var fun_maskCloud = function(image) {
var mask = image.select(['cloud']).lte(10);
return image.updateMask(mask);
};
var vsParam={band:['B2', 'B3', 'B4', 'B5', 'B6', 'B7'],min:0,max:0.3};
var maskCloud = ee.ImageCollection(calCloudScore)
.map(fun_maskCloud) ;
var maskCloud2=maskCloud.mean()
return maskCloud;
}
var image=remove_cloud(collection).mosaic().clip(roi);
function NDWI(img){
var nir = img.select('B4');
var green = img.select('B2');
var ndwi = img.expression(
'(B3-B5)/(B3+B5)',
{
'B5':nir,
'B3':green
});
return ndwi;
}
function EWI(img){
var swir1 = img.select('B6')
var nir = img.select('B5');
var green = img.select('B3');
var ewi = img.expression(
'(B3-B5-B6)/(B3+B5+B6)',
{
'B6':swir1,
'B5':nir,
'B3':green
});
return ewi;
}
var MNDWI = image.normalizedDifference(['B3', 'B6']).rename('MNDWI');
var ndbi = image.normalizedDifference(['B6', 'B5']).rename('NDBI');
var ndvi = image.normalizedDifference(['B5', 'B4']).rename('NDVI');
var ewi = EWI(image).rename('EWI');
var ndwi = NDWI(image).rename('NDWI');
var lswi = image.normalizedDifference(['B5','B6']).rename('LSWI')
var nbr2 = image.normalizedDifference(["B6", "B7"]).rename("NBR2")
var awei = image.expression(
'4*(green-SWIR1)-(0.25*NIR+2.75*SWIR2)',{
green:image.select('B3'),
NIR:image.select('B5'),
SWIR1:image.select('B6'),
SWIR2:image.select('B7'),
}).float().rename('AWEI')
image=image
.addBands(ndvi)
.addBands(ndbi)
.addBands(MNDWI)
.addBands(ndwi)
.addBands(ewi)
.addBands(lswi)
.addBands(awei)
.addBands(nbr2)
var classNames = city.merge(water).merge(tree).merge(crop).merge(bare);
var bands = ['B2', 'B3', 'B4', 'B5', 'B6', 'B7','MNDWI','NDBI','NDVI','EWI','NDWI','AWEI','LSWI',"NBR2"];
var training = image.select(bands).sampleRegions({
collection: classNames,
properties: ['landcover'],
scale: 30
});
var withRandom = training.randomColumn('random');
var split = 0.9;
var trainingPartition = withRandom.filter(ee.Filter.lt('random', split));
var testingPartition = withRandom.filter(ee.Filter.gte('random', split));
var classProperty = 'landcover';
var classifier = ee.Classifier.libsvm().train({
features: trainingPartition,
classProperty: 'landcover',
inputProperties: bands
});
var classified = image.select(bands).classify(classifier);
var test = testingPartition.classify(classifier);
var confusionMatrix = test.errorMatrix('landcover', 'classification');
print(year)
print('confusionMatrix',confusionMatrix);
print('overall accuracy', confusionMatrix.accuracy());
print('kappa accuracy', confusionMatrix.kappa());
Map.addLayer(classified,{}, "classified"+year);
Export.image.toDrive({
image: classified,
description: 'YR_cart'+year,
folder: 'YR_cart'+year,
scale: 30,
region: roi,
maxPixels:34e10
});
}
function main() {
var startYear = 2022;
var endYear = 2022;
for (var year=startYear; year<=endYear; year++) {
if (year >2012){l8class(year)}else{l57class(year)}
}
}
main();
I knew the study area was too big which lead to an error: "computed value is too large". Does anyone know how to solve this problem? better to revise based on this code if so.
Really appreciate it in advance!
To be honest I checked too many ideas but one of the effective ways I thought is to "break your study area into a bunch of tiles to calculate first, then combine all the results to get the final. However, It's just initial thinking that I didn't test.
Here is the link(you can ignore the comment in Chinese): https://code.earthengine.google.com/5d362229b959d96b9e4017571ba11a75

How to generate leaflet control from database

I wish to generate a custom dropdown filter, based on categories from a database.
How is this achieved?
In my example, this is (poorly) implemented with some hard coding and duplication.
var serviceOverlays = [
{name:"Cardiology", value:"cardiology"},
{name:"Opthamology", value:"opthamology"}
];
var oSelect = L.control({position : 'topright'});
oSelect.onAdd = function (map) {
var overlayParent = document.getElementById('new-parent'); // overlays div
var node = L.DomUtil.create('select', 'leaflet-control');
node.innerHTML = '<option value="cardiologist">Cardioligist</option><option value="opthamology">Opthamology</option>';
overlayParent.appendChild(node);
L.DomEvent.disableClickPropagation(node);
L.DomEvent.on(node,'change',function(e){
var select = e.target;
for(var name in serviceOverlays){
serviceOverlays[name].removeFrom(map);
}
serviceOverlays[select.value].addTo(map);
});
Fiddle
I created a Control for you:
L.Control.Select = L.Control.extend({
options: {
position : 'topright'
},
initialize(layers,options) {
L.setOptions(this,options);
this.layers = layers;
},
onAdd(map) {
this.overlayParent = L.DomUtil.create('div', 'leaflet-control select-control');
this.node = L.DomUtil.create('select', 'leaflet-control',this.overlayParent);
L.DomEvent.disableClickPropagation(this.node);
this.updateSelectOptions();
L.DomEvent.on(this.node,'change',(e)=>{
var select = e.target;
for(var value in this.layers){
this.layers[value].layer.removeFrom(map);
}
this.layers[select.value].layer.addTo(map);
});
return this.overlayParent;
},
updateSelectOptions(){
var options = "";
if(this.layers){
for(var value in this.layers){
var layer = this.layers[value];
options += '<option value="'+value+'">'+layer.name+'</option>';
}
}
this.node.innerHTML = options;
},
changeLayerData(layers){
this.layers = layers;
this.updateSelectOptions();
}
});
var oSelect = new L.Control.Select(serviceOverlays,{position : 'topright'}).addTo(map);
The data structure have to be:
var serviceOverlays = {
"cardiology": {name:"Cardiology", layer: cities},
"opthamology": {name:"Opthamology", layer: badCities}
};
Demo: https://jsfiddle.net/falkedesign/1rLntbo5/
You can also change the data dynamicl< with oSelect.changeLayerData(serviceOverlays2)

AddToSetEach operator won't return the correct query (Builders v 2.4.4)

I've some code to update an array (add to set). The code is currently using the legacy builder:
var data = new[] { 10, 20, 30 };
var fieldName = "data";
var oldWay = Update.AddToSetEach(fieldName , new BsonArray(data));
Console.WriteLine($"Old way:\n {oldWay.ToJson()} \n\n");
This works perfectly and prints:
Old way:
{ "$addToSet" : { "users" : { "$each" : [10, 20, 30] } } }
But when trying to use the new Builders class, I can't get it to work correctly. I'm using MongoDB.Driver 2.4.4. My code:
var data = new[] { 10, 20, 30 };
var fieldName = "data";
var newWay = Builders<BsonDocument>.Update.AddToSetEach(fieldName, new BsonArray(data)).ToJson();
Console.WriteLine($"New way:\n {newWay} \n\n");
The output is:
New way:
{ "_t" : "AddToSetUpdateDefinition`2" }
Any thoughts?
Thank you!
I don't think .ToJson() renders the string how you want it to on a FilterDefinition. It used to be a little easier in the old driver.
Someone wrote an extension method though:
public static class MongoExtensions
{
public static BsonDocument RenderToBsonDocument<T>(this UpdateDefinition<T> filter)
{
var serializerRegistry = BsonSerializer.SerializerRegistry;
var documentSerializer = serializerRegistry.GetSerializer<T>();
return filter.Render(documentSerializer, serializerRegistry);
}
}
Then you can
var data = new[] { 10, 20, 30 };
var fieldName = "data";
var newWay = Builders<BsonDocument>.Update.AddToSetEach(fieldName, new BsonArray(data));
var newWayJson = newWay .RenderToBsonDocument().ToJson();
Console.WriteLine($"New way:\n {newWayJson } \n\n");

How do query among many collections then write result to corresponding collection

If I want to query users' age > 18,
and export result to corresponding collection,
How could I do it by rewriteing the following script?
The following is psuedo code
source_collections = ["user_1", "user_2", ..., "user_999"]
output_collections = ["result_1", "result_2", ..., "result_999"]
pipeline = [
{
"$match":{"age": > 18}
}
{ "$out" : output_collections }
]
cur = db[source_collections].runCommand('aggregate',
{pipeline: pipeline,allowDiskUse: true})
The script you're looking for is something like:
var prefix_source = 'user_';
var prefix_output = 'result_';
var source_collections = [];
var output_collections = [];
var numCollections = 999;
for (var i = 1; i <= numCollections; i++) {
source_collections.push(prefix_source + i);
output_collections.push(prefix_output + i);
}
var pipeline = [{'$match': {age: {'$gt': 18}}}, {'$out': ''}]; for (var currentCollection = 0; currentCollection < source_collections.length; currentCollection++) {
pipeline[pipeline.length - 1]['$out'] = output_collections[currentCollection];
var cur = db[source_collections[currentCollection]].runCommand('aggregate', {pipeline: pipeline,allowDiskUse: true});
}
And while you're at it, the var cur = ... line could be simplified to
db[source_collections[currentCollection]].aggregate(pipeline, {allowDiskUse: true});
Note: I've added a piece that generates your arrays for you, as I'm sure you're not looking to write them by hand :D

get values from XML file in my Titanium Project

I am having the following .xml which i used in my android project.now,am trying to create the same in iPhone using titanium.Can some one tell me how to get this values from the xml file and show them in a table view.? if i give MainCategories the 3 values in it should appear in the table view.Also can some one tell me is there any other better option to achieve this?or can we use plist??thank you.
<string-array name="MainCategories">
<item>Acceleration</item>
<item>Angle</item>
<item>Area</item>
</string-array>
<string-array name="Acceleration_array">
<item>meter/sq sec</item>
<item>km/sq sec</item>
<item>mile/sq sec</item>
<item>yard/sq sec</item>
</string-array>
<string-array name="Angle_array">
<item>degree</item>
<item>radian</item>
<item>grad</item>
<item>gon</item>
</string-array>
Try This.
var result = [];
var fileName = 'arrays1.xml'; //save xml file
var tableView = Ti.UI.createTableView({ data : result, width : '100%', height : '100%' });
function readXML(fileName)
{
var file = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, fileName);
var xmltext = file.read().text;
var doc = Ti.XML.parseString(xmltext);
var parentNodeLength = doc.documentElement.getElementsByTagName('string-array').length;
for (var i = 0; i < parentNodeLength; i++) {
var attrValue = doc.documentElement.getElementsByTagName('string-array').item(i).attributes.getNamedItem('name').nodeValue;
if (attrValue === 'Angle_array') {
var parentNode = doc.documentElement.getElementsByTagName('string-array').item(i);
var subNodeLength = parentNode.getElementsByTagName('item').length;
for (var j = 0; j < subNodeLength; j++) {
var title = parentNode.getElementsByTagName('item').item(j).text;
var row = Ti.UI.createTableViewRow({
height : 110
});
var label = Ti.UI.createLabel({
height : Ti.UI.SIZE,
width : Ti.UI.SIZE,
text : title
});
row.add(label);
result.push(row);
}
}
}
}
readXML(fileName);
tableView.setData(result);
win1.add(tableView);
win1.open();
i am not familiar with titanium but maybe this should help you out:
var result = this.responseText;
var xml = Ti.XML.parseString(result);
var params = xml.documentElement.getElementsByTagName("member");
var name = xml.documentElement.getElementsByTagName("name");
var value = xml.documentElement.getElementsByTagName("string");
var data = [];
for (var i=0;i<params.item.length;i++) {
Ti.API.log('Param '+i+': Name: '+n.item(i).text);
Ti.API.log('Param '+i+': Value: '+v.item(i).text);
// Add to array
data.push({"name":n.item(i).text,"value":v.item(i).text});
}
copied from this link
for displaying it in uitableview, save your values in nsarray and display your array in tableview. here is a tutorial for UITableView.