I have a scenario in Javascript which runs and it gives me a result in Console. In this scenario, I need to insert 5 times a pair of different coordinates (for each time). If I copy-paste the same code 5 times, with different coordinates (which they are inserted in different variables each time), it doesn't run. Below, I put the same code twice with different coordinates as I try the same thing in Jsfiddle.
//first pair of coordinates:
let x= 38.041242 ,y= 23.679595
let myString = `https://maps.googleapis.com/maps/api/streetview?location=${x},${y}&size=300x300&pitch=90`
getBase64FromImageUrl(myString);
console.log(x,y);
//second pair of coordinates:
let z= 38.041310 ,f= 23.679684
let myString = `https://maps.googleapis.com/maps/api/streetview?location=${z},${f}&size=300x300&pitch=90`
getBase64FromImageUrl(myString);
console.log(z,f);
I suspect as #ThatBrianDude and #Archer have already stated that you are getting an error because you are defining the variable myString more than once. I would also recommend you create an array of objects instead of copy pasting the code over and over.
// Array of coordinates to run through.
let coordinates = [{x: 38.041242, y: 23.679595, {x: 38.041310, y: 23.679684}];
// List of images.
let images = coordinates.map((coordinate) => {
// URL based on coordinates.
return getBase64FromImageUrl(`https://maps.googleapis.com/maps/api/streetview?location=${coordinate.x},${coordinate.y}&size=300x300&pitch=90`);
});
Related
I try to use turf.js and its function intersect in my Leaflet project. My original question can be found here. The problem is I cannot get a proper polygon in order to call it. The idea is to get the waypoints of the calculated route make a polgyon out of them and check where they intersect with a given buffered area.
var testpoint = turf.point([9.9354, 49.799]);
var buffered = turf.buffer(testpoint, 50, {units: 'meters'});
var array = []
control._routes[0]['coordinates'].forEach(function(e){
array.push([e['lat'],e['lng']])
});
var test = turf.polygon(L.polygon([[array]]))
var intersection = turf.lineIntersect(buffered, test)
I am getting the following error message:
Uncaught Error: Input geometry is not a valid Polygon or MultiPolygon
Here I have to point out that, when using line.intersect(buffered,buffered) it is working correctly, therefore I am assuming that the buffered variable is correct.
When it comes down to the test variable, I have tried with no [], one pair and double pair. But they all result in the same problem. I also tried to pass the array to turf.polygon but it couldn`t be done.
I want to use Fusionchart to read my Firebase data and create a chart in my web app. but my Firebase DB has a wrong structure, so the Fusionchart can't get data (my Firebase config is right).
Following is the code that I write data to Firebase, num is a value increased in each loop. But as shown in the attached picture, the child's name is not added as a sequence number.
Another question is I don't want the unique key inside the child 1, just six various inside the child one is ok.
Any suggestions will be appreciated.
firebase.database().ref('testdata/User1').child(num).push({
x: posX,
y: posY,
MaxSpeed: maxSpeed,
steps: counter,
time: Timeperiod /1000,
speed: SpeedRecord,
});
If you don't want the push ID inside your pseudo-numeric keys, call set instead of push.
So:
firebase.database().ref('testdata/User1').child(num).set({
x: posX,
y: posY,
MaxSpeed: maxSpeed,
steps: counter,
time: Timeperiod /1000,
speed: SpeedRecord,
});
Your other problems seems (it's impossible to be certain, since you didn't include the code for the increment) to come from the fact that num is a string. If that is indeed the case, increment it with:
num = String(parseInt(num) + 1);
Using such numeric keys is an antipattern in Firebase though, so I'd usually recommend against using them. If you must, at least pad them til a certain length, so that you can sort/filter on them easily.
Something as simple as:
num = String(parseInt(num) + 1).padLeft(5, "0");
Will work on all modern browsers, and ensures that all keys-that-look-like-numbers-but-behave-like-strings will show up in the order you expect.
I am new to machine and new to tensorflow. As a method of learning, I have been following along with Dan Shiffman of the Coding Train as he shows how to create a color classifier.
I, however, wanted to do something different, so I collected data of hand-drawn shapes. Each shape has a label (square, circle, or triangle) and also has an array of 400 pixels (each picture was draw in a 20x20 grid). The data is stored in an object, which is contained within a JSON file.
Using p5.js's 'loadJSON' function, I can access the JSON file, iterate the entries, and create two arrays: 'shapes,' which is a an array of an array of pixels, and 'labels,' which is an array of the corresponding labels.
Below is the code, which utilizes p5's 'preload' and 'setup' functions.
let data;
let model;
// list of labels from which you can get the oneHot index
let labelList = [
"square",
"circle",
"triangle"
];
// function that is ran before setup
function preload() {
data = loadJSON('shapeData.json'); // loads the json file
}
// called after preload
function setup() {
let shapes = []; // an array of the pixels of hand-drawn shapes
let labels = []; // the corresponding label of each hand-drawn shape
// iterates over all of the entries in data
for (let record of data.entries) {
let pixels = record.pixels;
shapes.push(pixels);
labels.push(labelList.indexOf(record.label));
}
// ---------------- ERROR ------------------------
let xs = tf.tensor2d(shapes);
// these tensors work great
let labelsTensor = tf.tensor1d(labels, 'int32');
let ys = tf.oneHot(labelsTensor, 3);
}
The problem arises when I try to create the 'xs' as a tensor2d.
I get the error:
Error: Constructing tensor of shape (120800) should match the length of values (121881)
at assert (tfjs#0.11.7:2)
at new e (tfjs#0.11.7:2)
at Function.e.make (tfjs#0.11.7:2)
at Function.e.tensor (tfjs#0.11.7:2)
at Object.e.tensor2d (tfjs#0.11.7:2)
at setup (sketch.js:27)
at p5.<anonymous> (p5.js:46551)
at _runIfPreloadsAreDone (p5.js:46499)
at p5._decrementPreload (p5.js:46509)
at p5.js:59586
I have a total of 302 data points, each of which is an array of 400 binary numbers. Therefore the shape of the tensor should be [302, 400], and when I change the shape to something that is wrong (like [303, 401]) it gives an error saying that it should be [302, 400].
I am new to this whole process, so any help will be appreciated.
Thanks
I have found the fix! The separate script which downloads the data had an error. This created data of different lengths...
For instance, one shape had 400 pixels and another had 410 pixels. This difference created a problem within tensorflow. After removing the data points that did not have 400 pixels, the program worked just fine!
I have a two-dimensional grid, in which all tiles are defined by two coordinates x and y. I'm storing the tiles in an array like this var tiles = [];. Each tile is an object with an x and y property:
Tile = {
x: ...,
y: ...
}
For drawing purposes (canvas) I want to find out which tiles are adjacent to each other. I could do that by looping through each element and check if it is adjacent. Since that would take n^n number of accesses I don't think this is the right way to do it. I think there would be a more efficient algorithm.
I also thought that maybe storing the data in a different way would help, but again, I wouldn't know how.
You have 2 ways to create a grid :
Using a 2 dimensional Array which must be the easier thing for a grid
Store adjacent Tile of a Tile in it with something like that :
var tile0 = {
x:0, y:1
}
var tile1 = {
x:1,y:1, tileLeft : tile0
}
It can be useful if you want to create Pentagonal or Hexagonal... grid, ofcourse create your grid automatically with a for loop.
EDIT
A two dimensional array is simply an Array of Array
var arr = new Array()
for(var i = 0 ; i < 10 ; i++){
arr[i] = new Array()
}
Now you can set value like in a grid, for example :
arr[0][2] = {x:2,y:2} //It's a bit useless since indexes can be use for x and y
In that case, i have 10 Array stored in one Array so :
arr[10][0]
Will return following error : Uncaught TypeError: Cannot set property '2' of undefined, because index of arr are only define between 0 and 9.
With your data construct I can't see a way to check for all adjacent tiles without accessing each tile.
The "tried and true" two dimensional grid data construct would be...a simple two dimensional array. Then you can access tiles directly by their coordinates.
As far as I understand topojson.presimplify(JSON) in D3 adds Z coordinate to each point in the input topojson shape based on its significance, which then allows to use it for the dynamic simplification like in http://bl.ocks.org/mbostock/6245977
This method topojson.presimplify() takes quite a long time to execute on complicated maps, especially in Firefox which makes the browser unresponsive for few seconds.
Can it be baked directly into the topojson file via the command line as it is done with projections:
topojson --projection 'd3.geo.mercator().translate([0,0]).scale(1)' -o cartesian.topo.json spherical.topo.json
I found a workaround for this which is not completely as simple as I wanted but still achieves the same result.
After the topojson.presimplify(data) is called, data already holds the pre simplified geometry with added Z axis values.
Then I convert it to the JSON string and manually copy it to a new file with JSON.stringify(data)
Nevertheless these conversion to a JSON string has a problem with Infinity values which often occur for Z and with JSON.stringify method are converted to null. Also when there is a value for Z coordinate it is usually too precise and writing all decimal points takes too much space.
For that reason before converting data to a JSON string I trim the numbers:
// Simplifying the map
topojson.presimplify(data);
// Changing Infinity values to 0, limiting decimal points
var arcs = data.arcs;
for(var i1 = arcs.length; i1--;) {
var arc = arcs[i1];
for(var i2 = arc.length; i2--;) {
var v = arc[i2][2];
if(v === Infinity) arc[i2][2] = 0;
else {
arc[i2][2] = M.round(v * 1e9)/1e9;
}
}
}
This makes Infinity values to appear as exactly 0 and other values are trimmed to 9 decimal points which is enough for dynamic simplification to work properly.
Since such string is too long to easily print it for copying to the new json file it is much easier to store it in the localStorage of the browser:
localStorage.setItem(<object name>, JSON.stringify(data))
Then in Safari or Chrome open the developer console and in the tab Resources -> Local Storage -> <Website URL> the stored object can be found, copied and then pasted into a text editor.
Usually it is pasted as a <key> <value> pair, so one needs to remove from the beginning of the pasted string so that it starts from {.
Since Infinity values have been converted to 0, in the dynamic simplification function it should be taken into account so that points with Z = 0 are treated as Z = Infinity and are always plotted with any simplification area:
point: function(x, y, z) {
if (z===0 || z >= simplificationArea) {
this.stream.point(x, y);
}
}