How to connect spatial points across facets in r? - maps

I have made a map and split the data within two facets, I’d like to make a connection between points in the maps but the lines would need to cross the facets. I need to connect multiple points.
Like this plot:

Related

Combining multiple geo sources in Tableau

I want to create a filled map of administrative regions (districts, counties etc.) for a particular country where the intensity of color depends on some measure of these closed areas.
I have 2 data sources.
This contains the lat-long values and name of the area. This table contains all the points required to define a closed polygon for an administrative area.
Here I have a numeric value and lat-long positions of where this value was recorded.
Using tableau and the first data source, I could create a filled map of the administrative regions and assign arbitrary colors to them.
I want to use the second data source and use some aggregate, say AVG, on my numeric value that was recorded at lat-long values that fall in one of the polygons computed earlier and use them to color the polygons.
Is there some way to do this?
(I have not used Tableau in the past. But, I loved the way it can handle some basic plotting tasks. I have a feeling that it should be possible with Tableau, but could not find any tutorial on how to get this.)

Design advice on a view

I have a view (ObjectFlattenedView) that flattens data points from various tables and views containing everything that has to do with the object of interest. Then, Azure Search indexes the output of this view for my ui to later issue search queries to locate the relevant records. Object has various attributes (columns in the view) that we might want to search by. The view is quite large as far as the number of objects and also the varying sources of attributes so performance is a major concern.
I have a new attribute that I need to add to this flattened view. Each object has 0 to many records in this TableN which has the following structure
ObjectId SubNo SubValue
A Sub5 0
B Sub1 0
B Sub2 1
B Sub.. ..
B SubK 0
Now I need to add this new attribute (AttrN) so that I can index the object docs (as Azure Search calls it) containing this new attribute. The flattened view would be something like this:
ObjectId Attr1... AttrN
A abc..... {Sub5:0}
B abd..... {Sub1:0,Sub2:1,...SubK:0}
My dilemma is the following:
If I flat out add a cte that concatenates the different sub-values of the object, the performance of the view worsens by 200% to 700%. The cte uses mssql's stuff and for xml. The execution plan does show ~8% of the overall cost to be this for xml statement. I know there will be a performance impact as I join more tables/views into my flattened view but the order of magnitude that I am getting hit with is quite high.
If I outer join my ObjectFlattenedView directly to TableN, then my view will have records for objects that are equal to 1 to the number of records that the object has in TableN. This complicates the Azure Search result handling such as how many records to get from Search and do the pagination as objects can have 0 to M records coming from TableN.
Has anyone come across with a similar issue and do you have patterns that you can suggest for me to handle this situation either on the sql server side to feed Azure Search with proper rowset or on the Azure Search side to handle 0:M records per object (document)?
Not sure if the below will completely solve your problem, but it might help. A couple of observations:
Instead of creating a single uber-view flattening everything, you can set up multiple datasource/indexer pairs all writing into the same search index - as long as all of them agree on document id, you can merge the data and assemble your Azure Search documents piece-by-piece from multiple sources.
To handle arrays of values, Azure Search has Collection(Edm.String) field type. Since SQL doesn't support arrays natively, you can generate a string field in JSON array format (e.g., ["a", "b", "c"]) and use jsonArrayToStringCollection function as described in this article.
HTH!

Get projection limits from Postgis

I receive spatial queries to my API in lat/lon coordinate pairs. My spatial data is in projections that don't cover the entire globe, which follows that some queries are out of bounds.
I'd like to respond to wrong queries with helpful error message. Rather than try to find out some in GIS specifications or standards what boundaries each projection has (and getting correct lat/lon pairs from those), I'd like to know wether I could either ask the limits from Postgis or ask if specific point is within limits and in what way it's wrong. This way I could support many projections easily.
It looks like Postgis has this information because for wrong query it answers thus:
transform: couldn't project point (-77.0331 -12.1251 0):
latitude or longitude exceeded limits (-14)
I'm using Postgis through Geodjango distance query (django.contrib.gis.geos.geometry.GEOSGeometry.distance function).
PostGIS or PROJ.4 (follow this thread) don't have these bounds. Each projection's bounds are unique, and are traditionally published by the authority that designed the projection.
One of the primary sources for this data is from https://www.epsg-registry.org click "retrieve by code" and (e.g.) 27200, and view the "Area of Use" fields.
Much of the same information is repeated at (e.g.) http://epsg.io/27200 and look for "bounds".
If you need this data, I suggest you make a new table to collect it.

Find all lat/long pairs within a given distance from a lat/long pair

I have a database with millions of lat/long pairs. I would like to implement a function to gather all lat/long pairs within a specified distance from a given lat/long pair. Is there a better way to do this than by iterating over each pair in the database and computing the distance between that pair and the given pair? I'd like to avoid brute force if I can avoid doing so!
I would like to add that I will never be searching for lat/long pairs greater than 1 mile from the given lat/long pair.
Many databases support storage of spatial types directly, and include spatial queries. This will handle the distance computation correctly for you, as well as provide a far more efficient means of pulling the information.
For examples, see:
Spatial Data in SQL Server
Geometric types in PostgreSQL
MySQL Spatial Extensions
SpatiaLite
What you can do is cluster the database beforehand. In this case you would divide the database into, say, 3-mile clusters. Then when you do the search you only need to compare points within the same cluster.

Get info from map data file by coordinate

Imagine I have a map shape file (.shp) or osm xml, I'm able to see different kind of data from different layers in GIS oriented programs, e.g. ArcGIS, QGIS etc. But how can I get this info programmatically? Is there a specific library for that?
What I'm really looking for is a some kind of method getMapData(longitude, latitude) to get landscape/terrain info (e.g. forest, river, city, highway) in specified location
Thanks in advance for your answers!
It still depends what you want to achieve whether you are better off using raster or vector data.
If your are using your grid to subdivide an area as an array of containers for geographic features, then stick with vector data. To do this, I would create a polygon grid file and intersect it with each of your data layers. You can then add an ID field that represents the cell's location in the array (and hence it's relative position to a known lat/long coordinate - let's say lower left). Alternatively you can use spatial queries to access your data by selecting a polygon in your vector grid file and then finding all the features in your other file that are contained by it.
OTOH, if you want to do some multi-feature analysis based on presence/abscence then you may be better going down the route of raster analysis. My gut feeling from what you have said is that this is what you are trying to achieve but I am still not 100% sure. You would handle this by creating a set of boolean rasters of a suitable resolution and then performing maths operations on the set (add, subtract, average etc - depending on what questions your are asking).
Let's say you are looking at animal migration. Let's say your model assumes that streams, hedges and towns are all obstacles to migration but roads only reduce the chance of an area being crossed. So you convert your obstacles to a value of '1' and NoData to '0' in each case, except roads where you decide to set the value to 0.5. You can then add all your rasters together in one big stack and predict migration routes.
Ok that's a simplistic example but perhaps you can see why we need EVEN more information on what you are wanting to do.
Shapefiles or an osm xml file are just containers that hold geometric shapes. There are plenty of software libraries out there that let you read these files and extract the data. I would recommend looking at GDAL/OGR as a starting point.
A method like getMapData(longitude, latitude) is essentially a search/query function. You need to be a little more specific too, do you want geometries that contain the point, are within a distance of a point, etc?
You could find the map data using a brute force algorithm
for shape in shapefile:
if shape.contains(query_point):
return shape
Or you can use more advanced algorithms/data structures such as RTrees, KDTrees, QuadTrees, etc. The easiest way to get start with querying map data is to load it into a spatial database. I would recommending investigating PostgreSQL+PostGIS and SpatiaLite
You may also like to look at Spatialite and/or PostGIS which are two spatial enabled databses that you could use separately or in conjunction with GDAL/OGR.
I must echo Charles' request that you explain your use-case in more detail because the actual implementation will depend greatly on exactly what you are wanting to achieve. My reading of this is that you may want to convert your data into a series of aligned rasters which you can overlay and treat as a 3 dimensional array.

Resources