How to format a Javascript object as a polygon in Mysql - javascript

I want to store a Javascript object as a polygon in a Mysql database.
I can define start and end points of x and y to give me four corners:
var polygon = [
xstart +' '+ ystart,
xend +' '+ ystart,
xend +' '+ yend,
xstart +' '+ yend,
xstart +' '+ ystart
];
I can format this object as a string
polygon = "ST_GeomFromText('POLYGON(("+ polygon.toString() +"))'";
When I insert this into a Mysql database
INSERT INTO `caption` (`caption_id`, `caption_area`) VALUES (NULL, '\'POLYGON((0.28 0.33,0.35 0.33,0.35 0.45,0.28 0.45,0.28 0.33))\'')
the query fails
#1416 - Cannot get geometry object from data you send to the GEOMETRY field
How can I format the Javascript object, or the query, to correctly insert a polygon in to a database?

in mysql you must use ST_GeomFromText
polygon = "ST_GeomFromText('POLYGON("+ polygon.toString() +")')";
I am unsure what you need the number 0 at the end.

POLYGON is a Spatial Data Type from MySQL spatial extension defined in the Open Geospatial Consortium OpenGIS.
You can define a Spatial Data Type in MySQL for your four polygon. The following is an example using LINESTRING:
LINESTRING(0.28540775806607094 0.3356063323928521, 0.35407728596306665 0.3356063323928521, 0.35407728596306665 0.45764498813705906, 0.28540775806607094 0.45764498813705906, 0.28540775806607094 0.3356063323928521)
So, it stores caption_area as TEXT using:
SET #g = 'LINESTRING(0.28540775806607094 0.3356063323928521, 0.35407728596306665 0.3356063323928521, 0.35407728596306665 0.45764498813705906, 0.28540775806607094 0.45764498813705906, 0.28540775806607094 0.3356063323928521)';
INTO `caption` (`caption_id`, `caption_area`) VALUES (NULL,ST_AsText(ST_GeomFromText(#g)));
Also, an example with POLYGON:
SET #g = 'POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7, 5 5))';
INTO `caption` (`caption_id`, `caption_area`)
VALUES (NULL,ST_AsText(ST_GeomFromText(#g)));

Related

Output/Generate a set of points from individual equations - Python, JS, Matlab

I began with a set of coordinates, which I then approximated a function to represent them (Fourier series). The function produced is a sum of sin and cos waves:
0.3sin(2x) + 1.7(sin5x) + 1.8(sin43x)...
I would like to take this new function that I generated and produce a new set of coordinates. How can I generate points for every [INTEGER X Value] say from 0-400?
Note: I have 2 complex (2D) functions.
GOAL: Take a function --> Generate Points from this function for every whole integer.
This uses a function handle and (:) to force a column vector ((:).' forces a row vector).
The code simply uses the given equation (summing sines and cosines) to calculate a corresponding y coordinate for each given x coordinate.
% MATLAB R2018b
X = 0:400; % x = 0, 1, 2, ..., 400
fh = #(x) 0.3*sin(2*x) + 1.7*sin(5*x) + 1.8*sin(43*x);
Y = fh(X);
P = [X(:) Y(:)];
Note that size(P) returns 401 x 2. You'll see Y takes on whatever size X is, which is a row vector. X can be declared as as column vector with X = (0:400).' using .' which performs a transpose.
Recommend taking a look at MATLAB's documentation, specifically the Getting Started and Language Fundamentals.
Relevant MATLAB functions: sin, cos.
Matlab Code
X = 0:400;
fh = #(x) 0.3*sin(2*x) + 1.7*sin(5*x) + 1.8*sin(43*x);
Y = fh(X);
P = [X, Y]

Sort data based on my location and predefine city name

I am Using AngularJs and Ionic for Hybrid Mobile App development.
I am getting this data from API
[{"Location":"Brierfield"},
{"Location":"Centreville"},
{"Location":"Chelsea"},
{"Location":"Coosa Pines"},
{"Location":"Clanton"}]
With the help of below code I am getting my current location
var position = {};
var onSuccess = function(position2) {
console.log(position2.coords.latitude )
console.log(position2.coords.longitude)
position.latitude = position2.coords.latitude;
position.longitude = position2.coords.longitude;
$rootScope.$digest()
};
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
navigator.geolocation.getCurrentPosition(onSuccess, onError);
return position;
I have to check based on my current location and above data(Location) which city is near by me and based on that I need to sort my data in View.
It will be great if you can get lat & long data of the locations from the api. Getting your current position and location data you can use Haversine Formula to calculate the distance between two points.
In case you are not able to get position details from API then you have to get the coordinates of the location and use the Haversine Formula.
Hope this will solve your problem.

How do I produce an interpolation function given n data points?

I am faced with the problem of trying to interpolate values between points on a series plot i.e. my data looks like the following (please assume random x,y coordinates)
[[x0,y0], [1,1] ,[2,2], [2,3],.....[x,y]]
and from the interpolator, I would like to give it 1.5 and the interpolator function should return 1.5 for this case. In other cases where data is random, it should find a best fit for the given set of points and return the y value for the given x value
Is this possible using d3 interpolate*** functions?
Thanks
Although you could do this with d3 interpolators, it would probably be easier to use a muli-part linear scale.
Usually, linear scales have a two-value domain and a two-value range, and all other values are calculated from the straight line between the start and end points of domain and range. However, you can set both domain and range to an array of many values (so long as both arrays are the same length), and the scale will act as a series of straight-line relationships for each section of the domain.
In other words, if you use your array of x-values as the scale's domain, and your array of y-values as the range, then you can input any x value and the scale will return the linear interpolation between adjacent y values. For values outside your points, it will extrapolate the initial or final linear relationship:
var points = [
[0,10],
[1,32],
[2,14],
[3,15]
];
var multiLine = d3.scale.linear()
.domain(
points.map(function(p){return p[0];})
)
.range (
points.map(function(p){return p[1];})
);
document.body.innerHTML =
"Line at 0.0: " + multiLine(0) + "<br/>" +
"Line at 1.0: " + multiLine(1) + "<br/>" +
"Line at 1.5: " + multiLine(1.5) + "<br/>" +
"Line at 2.3: " + multiLine(2.3) + "<br/>" +
"Line at 3.0: " + multiLine(3) + "<br/>" +
"Line at 4.0: " + multiLine(4) ;
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
Note that you'll need to make sure that your points are sorted by x-value in order for this to work as expected.

Updating domain values of multiline graph after line drag event

I am developing a simple multi-line graph with dual time axes and zooming/dragging features. Please take a look at my JSFiddle.
I am trying to implement the drag feature on the line graph, whereupon dragging a particular line will result in its respective axis also getting updated. Every time the drag is applied to the graph, I am trying to update the domain values of its respective axis, and redraw both the axis and the line graph.
Here is the logic that I implemented to update the domain values (referenced from a D3 Example):
var mousePoint = d3.mouse(this);
x1 = x1Scale.domain()[0],
x2 = x1Scale.domain()[1],
console.log("x1 = "+x1+", x2 = " +x2);
xextent = x1 - x2;
x1 += mousePoint[0];
x2 += mousePoint[0];
var newDomain = [x1, x2];
x1Scale.domain(newDomain);
When I implement this logic, I get a NaN error. Is this the correct way to update the domain values after the drag? If so, how do I solve the NaN error and achieve the desired functionality?
It is important to convert numbers into date objects, there was a typo in your code (data1[0].time instead of data1[0].date). Also you shouldn't multiply by 1000, since your data was already in milliseconds.
In your drag code, it is also important to convert your date objects back to numbers, in order that += will work on them. Of course you also need to convert them back to date when setting the domain again.
function draggedData1(d) {
console.log("dragging of data1 going on!!")
var mousePoint = d3.mouse(this);
var x1 = x1Scale.domain()[0].valueOf(); //date to number
var x2 = x1Scale.domain()[1].valueOf();
var xextent = x1 - x2;
x1 += mousePoint[0];
x2 += mousePoint[0];
var newDomain = [new Date(x1), new Date(x2)]; //number back to date
x1Scale.domain(newDomain);
redraw();
zoomBottom.x(x1Scale);
zoom.x(x2Scale);
}
I've created a fiddle with the full code and fixes here:
http://jsfiddle.net/pb3cod6q/2/

Why does esri.geometry.getLength work wrong?

The function http://help.arcgis.com/en/webapi/javascript/arcgis/jsapi/#namespace_geometry/esri.geometry.getLength should calculate distances between points.
When I am trying to calculate distance between points having 100 meters between them
p1=new esri.geometry.Point(3997030.6690969253, 7444299.320646087, new esri.SpatialReference({ wkid: 102113 }));
Object
p2=new esri.geometry.Point(3996951.455397143, 7444142.154020177, new esri.SpatialReference({ wkid: 102113 }));
Object
esri.geometry.getLength(p1, p2)
176.00045037719127
I am getting 176 which is wrong. Projection is Web Mercator (WKID 102113).
It looks like it's just calculating the simple euclidean map distance between the two points (3997030,7444299) and (3996951, 7444142).
a^2 + b^2 = c^2
where a = (3997031 - 3996951) and b = (7444299 - 7444142)
c = 176
So 176 is the map distance, what you want is the real world ground distance. You'll probably have to use a GeometryService to accomplish this, I don't think the Javascript can do it on it's own. Here's ESRI's sample of it: https://developers.arcgis.com/javascript/jssamples/util_distance.html

Categories

Resources