Determine number of points within geographic subdivisions? - d3.js

I have a map of Russia, showing its subdivisions.
I have points plotted on that map. How do I determine how many points are located within the boundaries of each subdivision?

Related

how to count (ONLY ONCE) common latitude or longitude lying in the edge between two blocks of grid

I built a grid that includes the US map. The grid consists of latitudes and longitudes that represent small rectangles inside the US map. A small rectangle consists of many latitudes and longitudes.
From a data set that contains the population, I could reside each part of data in its place on the US grid based on their latitude and longitude.
My goal is to let the user to enter a queryBorders (inside the gird) to get the accurate population.
The problem is some points' (or population)latitudes and longitudes live in the border of two neighbors, which cause these points to get counted more than one time. This gives inaccurate results.
In the illustration above, how do I get the accurate result (excluding the repeated points) for the queryBorders (a,b,c,d)?
(getting help from "How to scale down a range of numbers with a known min and max value" to get space for each block!)
Thanks in advance.

Calculating isobars

If I have a set of readings (eg pressures) taken at lat-long points what are the algorithm(s) can I use to determine isobars? The recorded positions are not on any fixed pattern, and we can assume I have a few hundred over a reasonable area.

Randomly place objects USING ALPHA MAP

I want to randomly place objects USING ALPHA MAP (only black color, not grayscale). The black areas on the map are using to determine where we CAN place an object.
Why do I want to do this? For example, we have some terrain with a river. We want to randomly place a chest on that terrain and the point is we want our chest NOT TO BE IN THE RIVER. We have a special location to place that chest and that location can have a very complex structure.
Simple map with lake and river:
Black/white map of the location for placing objects:
Of course, we can just take random points with Random.Range() and check each point by comparing it with pixel values: "Is the point on the black area?" BUT if we would have a very small (<10% of total area) and complex "available" area (for example, islands in the swamp) then there will be a very large amount of "garbage" points. Therefore it is very inefficient method.
Does the quick and performance technique exist to get desirable amount of "available" points?
In theory you could segment the alpha map by color to get a geometric polygonal representation of the regions, then you could generate a point algorithmically inside a set of black/white polygons.
But If you're willing to trade memory for speed, there's a much simpler solution: just represent the alpha map as two arrays(black & white) of pixels' coordinates and then randomly pick a point from a needed array.

Generate adjacent polygons from interior points

I have a set of points (UK full postcode centroids). There is a hierarchical relationship in the postcodes to postcode sectors and postcode districts. The original sectors and districts are contiguous. I wish to derive approximate boundaries for the sectors and districts such that any part of country falls into exactly one sector and exactly one district, all resulting polygons should ideally be contiguous and (obviously?) all original points should be in the appropriate polygons.
Is there some appropriate algorithm? Better yet, is there some appropriate implementation?
I think I must have explained it poorly since I don't think that answers my question.
Let's just talk about sectors since the answer would also apply to districts.
There are 1.8m coordinates. Consider each of these is tagged with a postcode such as "SG13 7AT" The postcode tag can itself reflects the postcode-sector-district structure - the sector in this case is "SG13 7"
There is no other data than these points and their postcode tags.
I know that there exists a boundary that defines the sector. However, this boundary data is not freely available. Each postcode point is known to be inside its true sector boundary.
What I want is to recreate approximations of the sector boundaries such that the points fall within the newly created polygons and so that the polygons I create are contiguous. These boundaries will not be an accurate reflection of the originals, but they are good enough for my purposes.
To obtain the partition of the plane into sectors, according to the sampled postal codes, use the Voronoi diagram calculated on the full set of points, then assign each diagram cell to the sector containing the cell's site.
I am illustrating this with an example on two sectors, red and blue. Say your initial data is this:
Then after computing the Voronoi diagram, the division to cells will look as follows. I outlined the boundary between the red and blue sectors. Note that they are both unbounded, but that's just because the data doesn't include other sectors.
Now to my answer before you clarified things...
What you need is a data structure for "Point Location Queries": given a subdivision of space (in your case, the plane) and a query point, find the object that contains the query point. There are efficient algorithms (log(n) query time) for doing that on lines, segments, and polygons, and they have been implemented in the computational geometry library CGAL.
Note that I used the CGAL 2D triangulation demo to illustrate the solution
Check out this link for the documentation of point location queries.

Binary search algorithm for 2 dimensional approximite data

Here is my specific problem. I need to write an algorithm that:
1) Takes these 2 arrays:
a) An array of about 3000 postcodes (or zip codes if you're in the US), with the longitude and latitude of the center point of the areas they cover (that is, 3 numbers per array element)
b) An array of about 120,000 locations, consisting of longitude and latitude
2) Converts each location to the postcode whose centerpoint is closests to the given longitude and latitude
Note that the longitudes and latitudes of the locations are very unlikely to precisely match those in the postcodes array. That's why I'm looking for the shortest distance to the center point of the area covered by the postcode.
I know how to calculate the distance between two longitude/latitude pairs. I also appreciate that being closests to the center point of an area covered by a postcode doesn't necessarily mean you are in the area covered by that postcode - if you're in a very big postcode area but close to the border, you may be closer to the center point of a neighbouring postcode area. However, in this case I don't have to take this into account - shortest distance to center point is enough.
A very simple way to solve this problem would be to visit each of the 120,000 locations, and find the postcode with the closest centerpoint by calculating the distance to each of the 3000 postcode centerpoints. That would mean 3000 x 120,000 = 360,000,000 distance calculations though.
If postcodes and locations were in a one-dimensional space (that is, identified by 1 number instead of 2), I could simply sort the postcode array by its one-dimensional centerpoint and then do a binary search in the postcode array for each location.
So I guess what I'm looking for is a way to sort the two dimensional space of longitudes and latitudes of the postcode center points, so I can perform a two dimensional binary search for each location. I've seen solutions to this problem, but those only work for direct matches, while I'm looking for the center point closests to a given location.
I am considering caching solutions, but if there is a fast two-dimensional binary search that I could use, that would make the solution much simpler.
This will be part of a batch program, so I'm not counting milli seconds but it can't take days either. It will run once a month without manual intervention.
You can use a space-filling-curve and a quadkey instead of a quadtree or a spatial index. There are some very interesting sfc like the hilbert curve and the moore curve with very interesting patterns.

Resources