Google distances using C# - Over-Query-Limit - google-maps

I am trying to fetch google distances in gridview RowDataBound with a force sleep of 1000ms,Nothing helping,Am getting correct distance for the first query,ie the first row of the gridview, all others i get 'Over-Query-Limit' for content variable ,I want to know three things:
Is there any solution for this situation.
Is google limiting queries per day OR
Is google limiting queries per second ?
public int getDistance(string origin, string destination)
{
System.Threading.Thread.Sleep(1000);
int distance = 0;
string url = "http://maps.googleapis.com/maps/api/directions/json?origin=" + origin + "&destination=" + destination + "&sensor=false";
string requesturl = url;
string content = fileGetContents(requesturl);
JObject o = JObject.Parse(content);
try
{
distance = (int)o.SelectToken("routes[0].legs[0].distance.value");
return distance;
}
catch
{
return distance;
}
return distance;
}

Ok,As above 2500 queries per day makes google search a paid service i went for another logic,We can calculate distances without google.com as follows :
public decimal calcDistance(decimal latA, decimal longA, decimal latB, decimal longB)
{
double theDistance = (Math.Sin(DegreesToRadians(latA)) *
Math.Sin(DegreesToRadians(latB)) +
Math.Cos(DegreesToRadians(latA)) *
Math.Cos(DegreesToRadians(latB)) *
Math.Cos(DegreesToRadians(longA - longB)));
return Convert.ToDecimal((RadiansToDegrees(Math.Acos(theDistance)))) * 69.09M * 1.6093M;
}
public double DegreesToRadians(decimal degrees)
{
return Convert.ToDouble((Convert.ToDecimal(Math.PI) / 180.0M) * degrees);
}
public double RadiansToDegrees(double radians)
{
return Convert.ToDouble((180.0M / Convert.ToDecimal(Math.PI)) * Convert.ToDecimal(radians));
}

Related

Finding the minimum and maximum value from an array in a JSON File in p5.js

I have a JSON data showing the bus shelters of Gold Coast
this is my data = https://data.gov.au/geoserver/gold-coast/wfs?request=GetFeature&typeName=ckan_be880d63_175a_4383_b0f2_ef88b831eba9&outputFormat=json
from there I want to find out the minimum and maximum latitude and longitude using p5.js and plot them on the graph
here is my code up until now
function preload(){
shelters = loadJSON('https://data.gov.au/geoserver/gold-coast/wfs?request=GetFeature&typeName=ckan_be880d63_175a_4383_b0f2_ef88b831eba9&outputFormat=json')
}
function setup() {
createCanvas(400, 400`enter code here`);
print(shelters)
noLoop()
}
function draw() {
background(220);
bus = shelters.features
max_min()
}
function max_min(){
for(let i = 0; i < bus.length; i ++ ){
latitudes = createDiv('h1', bus[i].geometry.coordinate)
max_lat = Math.max(latitudes)
}
console.log(...max_lat)
}
I am having trouble finding the max and min latitude and longitude
You have a few things which can't work in your code, let's first have a look at that:
First, in your createCanvas(400, 400``enter code here``); lines you have a syntax error here, you need to remove the enter code here string.
Then in your max_min() function, you iterate on all the buses and get bus[i].geometry.coordinate. My understanding is that this is an array containing the latitude and longitude of the bus, so using Math.max on this array will return the max value between the latitude and the longitude of the current bus, which is not what you want.
You have several options to do what you want. If you really want to use Math.max() and Math.min() and are familiar with map you could:
Build an array of all the latitudes: bus.map(b => b.geometry.coordinates[0]) i.e take all the items in the bus array and transform that to an array with only the first item of each buse's geometry.coordinates property. (replace [0] by [1] to get the longitude)
Deconstruct it with the ... operator because Math.max() doesn't take an array but a list of arguments
And assign that to a value, you function would look like this:
function max_min() {
const maxLat = Math.max(...bus.map(b => b.geometry.coordinates[0]));
const minLat = Math.min(...bus.map(b => b.geometry.coordinates[0]));
const maxLong = Math.max(...bus.map(b => b.geometry.coordinates[1]));
const minLong = Math.min(...bus.map(b => b.geometry.coordinates[1]));
return { maxLat, minLat, maxLong, minLong };
}
Here the code is short but you iterate 4 times over the bus array, which is not especially efficient. So you could also take another approach where you would create a short helper function to get the latitude and longitude of a bus:
// Given a feature return its latitude and longitude in an object
function getLatLong(feature) {
const [lat, long] = feature.geometry.coordinates;
return { lat, long };
}
And then iterate over all the buses and given these information update the min and max values like this:
// Find the max and min longitude and latitude and return them in an object
function max_min() {
// Use the first feature of the list to initialize the max and min values
const { lat: firstLat, long: firstLong } = getLatLong(bus[0]);
let maxLat = firstLat;
let minLat = firstLat;
let maxLong = firstLong;
let minLong = firstLong;
// Iterate through all the features to update the max and min values
for (let i = 1; i < bus.length; i++) {
const { lat, long } = getLatLong(bus[i]);
if (lat > maxLat) {
maxLat = lat;
}
if (lat < minLat) {
minLat = lat;
}
if (long > maxLong) {
maxLong = long;
}
if (long < minLong) {
minLong = long;
}
}
// Return the updated max and min values
return { maxLat, minLat, maxLong, minLong };
}
Finally in draw() you can do whatever you want with them, for the example I only write them in the canvas:
function draw() {
background(220);
// Get the max and min values with our helper function
const { maxLat, minLat, maxLong, minLong } = max_min();
// Show the values
text(`max lat ${maxLat}`, 0, 10);
text(`min lat ${minLat}`, 0, 30);
text(`max long ${maxLong}`, 0, 50);
text(`min long ${minLong}`, 0, 70);
}
I created this codepen with the complete code working so that you can have a look at how the parts work together.

Estimote: Beacon Manager Ranging Questions

The following is code being used in an app I am working on. The app will try to detect an Estimote beacon and then output in Android notifications the distance of the beacon. The issue I am running into is that the notification is being sent every second, when it should be sent every 1/10th of a second - from setting the foreground scan period. Changing the advertising interval of the beacon didn't seem to have much of an effect either. Is there something I am missing with this?
beaconManager.setBackgroundScanPeriod(100,0);
beaconManager.setForegroundScanPeriod(100,0);
beaconManager.setRangingListener(new BeaconManager.RangingListener() {
int count = 0;
#Override
public void onBeaconsDiscovered(Region region, List<Beacon> beacons) {
if (beacons.size() < 1) {
return;
}
count++;
String distance = "" + getDistance(beacons.get(0).getRssi(), beacons.get(0).getMeasuredPower());
String beaconName = beacons.get(0).getName();
showNotification(count + " Name + Distance", beaconName + ": " + distance);
}
});

Square root calculation

want to calculate square root of given input so i used this code
var no1:Number = 2;
var no2:Number; //input for calculation
var total:Number;
total=Math.pow(no2,(1/no1));
its working , but i faced problem such as:-
if i give
no2 = 25;
then it shows
total=4.9999999
to overcome this problem i used below code
total=Math.ceil(Math.pow(no2,(1/no1)));
but its okey for 25 .
total = 5
problem was if i give 21,22,23,24
for all this input it shows 5
so is there any other solutions ???
If you want to take the nth root. You can feed the output of your function into arbitrary rounding function like this:
/**
* Rounds input number to specified number of decimal places. For example
* round(4.568, 2) will return 4.57, and round(4.99, 2) will return 5.
*/
function round(num:Number, toDecimalPlaces:uint) {
var factor:uint = Math.pow(10, toDecimalPlaces);
return Math.round(num * factor) / factor;
}
/**
* Returns nth root to two decimal places.
*/
function nthRoot(n:uint, num:Number) {
return round(Math.pow(num, (1 / n)), 2);
}
There's a function for that.
var total:Number = Math.sqrt(no2);
my actual code was like this
var str:String=view.numDisplay.text;//input string
var power:Number = Number(inString.charAt(inString.indexOf('√') - 1));//to get the value before √ i.e no1
var no1:Number;
var no2:Number;
var total:Number;
if(power)
no1=v;
else
no1=2;
no2=getSubTerm();//method to find no2 it returns the number for calculation
total=Math.ceil(Math.pow(no2,(1/no1)));

Google Spreadsheet batch update very slow

I'm updating a google spreadsheet and it takes about 30 seconds to update 100 cells.
I'm using the code described in the answer in the following question
Is this normal? This just seem to be horribly slow and nearly impossible to work with. Are there any options out there to speed up this process?
The answer was in the provided link
I changed:
private CellEntry createUpdateOperation(URL cellFeedUrl, int row, int col, String value) throws ServiceException, IOException {
String batchId = "R" + row + "C" + col;
URL entryUrl = new URL(cellFeedUrl.toString() + "/" + batchId);
CellEntry entry = this.service.getEntry(entryUrl, CellEntry.class);
entry.changeInputValueLocal(value);
BatchUtils.setBatchId(entry, batchId);
BatchUtils.setBatchOperationType(entry, BatchOperationType.UPDATE);
return entry;
}
into:
private CellEntry createUpdateOperation(URL cellFeedUrl, int row, int col, String value) throws ServiceException, IOException {
String batchId = "R" + row + "C" + col;
CellEntry batchEntry = new CellEntry(row, col, value);
batchEntry.setId(String.format("%s/%s", cellFeedUrl.toString(), batchId));
BatchUtils.setBatchId(batchEntry, batchId);
BatchUtils.setBatchOperationType(batchEntry, BatchOperationType.UPDATE);
return batchEntry;
}
300 cells edited in +/-20 seconds now so it's better...

Google maps - how to get building's polygon coordinates from address?

How to implement the following:
User defines an address
User defines a color
Service searches for a corresponding building on the google map
Service fills the found building on the map with the color
I know how to:
1.find lat/long of the address
2.draw the polygon
So, to do the task I need to get polygon coordinates of building from address. How to?
(1) Acquire image tile
(2) Segment buildings based on pixel color (here, 0xF2EEE6).
(3) Image cleanup (e.g. erosion then dilation) + algorithm to acquire pixel coordinates of polygon corners.
(4) Mercator projection to acquire lat/long of pixel
You can convert the address to geographic coordinates by the use of the Google Geocoding API.
https://maps.googleapis.com/maps/api/geocode/json?address=SOME_ADDRESS&key=YOUR_API_KEY
Then, you can use Python and a styled static map to obtain the polygon of the building (in pixel coordinates) at some location:
import numpy as np
from requests.utils import quote
from skimage.measure import find_contours, points_in_poly, approximate_polygon
from skimage import io
from skimage import color
from threading import Thread
center_latitude = None ##put latitude here
center_longitude = None ##put longitude here
mapZoom = str(20)
midX = 300
midY = 300
# Styled google maps url showing only the buildings
safeURL_Style = quote('feature:landscape.man_made|element:geometry.stroke|visibility:on|color:0xffffff|weight:1')
urlBuildings = "http://maps.googleapis.com/maps/api/staticmap?center=" + str_Center + "&zoom=" + mapZoom + "&format=png32&sensor=false&size=" + str_Size + "&maptype=roadmap&style=visibility:off&style=" + safeURL_Style
mainBuilding = None
imgBuildings = io.imread(urlBuildings)
gray_imgBuildings = color.rgb2gray(imgBuildings)
# will create inverted binary image
binary_imageBuildings = np.where(gray_imgBuildings > np.mean(gray_imgBuildings), 0.0, 1.0)
contoursBuildings = find_contours(binary_imageBuildings, 0.1)
for n, contourBuilding in enumerate(contoursBuildings):
if (contourBuilding[0, 1] == contourBuilding[-1, 1]) and (contourBuilding[0, 0] == contourBuilding[-1, 0]):
# check if it is inside any other polygon, so this will remove any additional elements
isInside = False
skipPoly = False
for othersPolygon in contoursBuildings:
isInside = points_in_poly(contourBuilding, othersPolygon)
if all(isInside):
skipPoly = True
break
if skipPoly == False:
center_inside = points_in_poly(np.array([[midX, midY]]), contourBuilding)
if center_inside:
# approximate will generalize the polygon
mainBuilding = approximate_polygon(contourBuilding, tolerance=2)
print(mainBuilding)
Now, you can convert the pixel coordinates to latitude and longitude by the use of little JavaScript, and the Google Maps API:
function point2LatLng(point, map) {
var topRight = map.getProjection().fromLatLngToPoint(map.getBounds().getNorthEast());
var bottomLeft = map.getProjection().fromLatLngToPoint(map.getBounds().getSouthWest());
var scale = Math.pow(2, map.getZoom());
var worldPoint = new google.maps.Point(point.x / scale + bottomLeft.x, point.y / scale + topRight.y);
return map.getProjection().fromPointToLatLng(worldPoint);
}
var convertedPointsMain = [];
for (var i = 0; i < pxlMainPolygons[p].length; i++) {
var conv_point = {
x: Math.round(pxlMainPolygons[p][i][1]),
y: Math.round(pxlMainPolygons[p][i][0])
};
convertedPointsMain[i] = point2LatLng(conv_point, map);
}
console.log(convertedPointsMain);
Might I humbly suggest you use OpenStreetMaps for this instead ?
It's a lot easier, because then you can use the OverPass API.
However, polygons might not match with google-maps or with state survey.
The latter also holds true if you would use google-maps.
// https://wiki.openstreetmap.org/wiki/Overpass_API/Overpass_QL
private static string GetOqlBuildingQuery(int distance, decimal latitude, decimal longitude)
{
System.Globalization.NumberFormatInfo nfi = new System.Globalization.NumberFormatInfo()
{
NumberGroupSeparator = "",
NumberDecimalSeparator = ".",
CurrencyGroupSeparator = "",
CurrencyDecimalSeparator = ".",
CurrencySymbol = ""
};
// [out: json];
// way(around:25, 47.360867, 8.534703)["building"];
// out ids geom meta;
string oqlQuery = #"[out:json];
way(around:" + distance.ToString(nfi) + ", "
+ latitude.ToString(nfi) + ", " + longitude.ToString(nfi)
+ #")[""building""];
out ids geom;"; // ohne meta - ist minimal
return oqlQuery;
}
public static System.Collections.Generic.List<Wgs84Point> GetWgs84PolygonPoints(int distance, decimal latitude, decimal longitude)
{
string[] overpass_services = new string[] {
"http://overpass.osm.ch/api/interpreter",
"http://overpass.openstreetmap.fr/api/interpreter",
"http://overpass-api.de/api/interpreter",
"http://overpass.osm.rambler.ru/cgi/interpreter",
// "https://overpass.osm.vi-di.fr/api/interpreter", // offline...
};
// string url = "http://overpass.osm.ch/api/interpreter";
// string url = "http://overpass-api.de/api/interpreter";
string url = overpass_services[s_rnd.Next(0, overpass_services.Length)];
System.Collections.Specialized.NameValueCollection reqparm = new System.Collections.Specialized.NameValueCollection();
reqparm.Add("data", GetOqlBuildingQuery(distance, latitude, longitude));
string resp = PostRequest(url, reqparm);
// System.IO.File.WriteAllText(#"D:\username\Documents\visual studio 2017\Projects\TestPlotly\TestSpatial\testResponse.json", resp, System.Text.Encoding.UTF8);
// System.Console.WriteLine(resp);
// string resp = System.IO.File.ReadAllText(#"D:\username\Documents\visual studio 2017\Projects\TestPlotly\TestSpatial\testResponse.json", System.Text.Encoding.UTF8);
System.Collections.Generic.List<Wgs84Point> ls = null;
Overpass.Building.BuildingInfo ro = Overpass.Building.BuildingInfo.FromJson(resp);
if (ro != null && ro.Elements != null && ro.Elements.Count > 0 && ro.Elements[0].Geometry != null)
{
ls = new System.Collections.Generic.List<Wgs84Point>();
for (int i = 0; i < ro.Elements[0].Geometry.Count; ++i)
{
ls.Add(new Wgs84Point(ro.Elements[0].Geometry[i].Latitude, ro.Elements[0].Geometry[i].Longitude, i));
} // Next i
} // End if (ro != null && ro.Elements != null && ro.Elements.Count > 0 && ro.Elements[0].Geometry != null)
return ls;
} // End Function GetWgs84Points
I've been working on this for hours, the closest I have come is finding a request uri that returns a result with a polygon in it. I believe it specifies the building(boundary) by editids parameter. We just need a way to get the current editids from a building(boundary).
The URI I have is:
https://www.google.com/mapmaker?hl=en&gw=40&output=jsonp&ll=38.934911%2C-92.329359&spn=0.016288%2C0.056477&z=14&mpnum=0&vpid=1354239392511&editids=nAlkfrzSpBMuVg-hSJ&xauth=YOUR_XAUTH_HERE&geowiki_client=mapmaker&hl=en
Part of the result has what is needed:
"polygon":[{"gnew":{"loop":[{"vertex":[{"lat_e7":389364691,"lng_e7":-923341133},{"lat_e7":389362067,"lng_e7":-923342783},{"lat_e7":389361075,"lng_e7":-923343356},{"lat_e7":389360594,"lng_e7":-923342477},
I was intrigued on this problem and wrote a solution to it. See my github project.
The Google Maps API contains a GeocoderResults object that might be what you need. Specifically the data returned in the geometry field.