xarray add cyclic point to unstructured grid - python

Has someone already added an cyclic point to an unstructured grid? I have an xarray with a lon lat grid and data points and I need to add an cyclic point in an certain intervall. Sadly cartopy needs equally spaced data points and now I'm struggling to find a way to do it on my own.
In the clon/clat are my coordinates and I need to add the cyclic point together with the corresponding datapoints.
I can't think of any easy way to do that. I'd appreciate any help.

Related

How to find points(latitude, longitude) at a fixed distance from the coast line?

I have latitude, longitude and geohash data for some places in a city near some sea(ex. Miami). I want to know which of these places are near the coast line(lets say within 100 m of the coastline). How can I approach this problem?
Data I have :
Name of the place, latitude of the place, longitude of the place, geohash of the place, global coastline data(shape file, but this is not accurate)
Approach Tried :
I downloaded the coastline data from here but this data is not very accurate. When I plot the shape file along with the lat, long of my original places data, I found that many places are lying outside the shorelines which is because the shorelines have been approximated by line segments as we zoom into the map(check the image attached).
Approach I am thinking of trying :
I am thinking of finding all geohashes(level 6) that cut the shoreline and then if the place is present inside these geohashes, I will classify them as near-coast places. Do you think this approach is good and will give desired results? Also I am really sure how I am going to find the geohashes that cut the polygon.
How big of a city is it? If you want a rought aproximation, you could first turn your wgs84 coordinates into an X,Y plane in UTM and then simply try to get an inequality by approximating the coastline to a straight line and figuring which side of the equation you want to keep. You could do this with more lines to make a better representation of the coastline and then offset them by 100 meters to get a polygon
One way to approach this is if you already got the coastline as a collection of points as can be found on the internet as shp files for example.
Iterate over these points and calculate the distance to the place and take the minimum.
If it is less than 100m you got a match.

When to transform GPS co-ordinates

I have no real experience with GIS data, so when what I believed to be a simple problem has turned out to have more subtleties to it, I am dangerously unprepared!
I want to be able to classify a GPS position as inside/outside a polygon defined by GPS co-ordinates. It turns out this is the well-known (but not to me) point-in-polygon problem. I have read many questions/answers on https://gis.stackexchange.com/ (and here e.g. this).
Shapely seems a good solution, but assumes the co-ordinates are on the same cartesian plane, i.e. not GPS? So I would first need to transform my GPS points to UTM points.
Do I need to introduce this extra step, however, if the points being compared (i.e. the point and the polygon) are always going to be naturally within the same UTM zone. They should always be within the same town/city, so can I just leave them as GPS and use the lat/long co-ordinates in Shapely?
I also came across this UTM-WGS84 converter so I could convert my lat/long pairs using this package, and then use those UTM pairs in Shapely, but I would like to avoid any extra dependencies where possible.
Point-in-polygon already assumes a 2D restriction, and GPS coordinates are 3D. Right away, that gets you in trouble.
A simple workaround is to discard the GPS height, reducing it to 2D surface coordinates. Your next problem is that that your 2D surface is now a sphere. On a spherical surface, a polygon divides the surface in two parts, but there is no obvious "inside". There's a left-hand side and a right-hand side which follows from the order of points in the polygon, but neither side is the obvious "inside". Consider the equator as a trivial polygon - which hemisphere is "inside" the equator?
Next up is the issue of the polygon edges. By definition, these are straight, i.e. line segments. But lines on a spherical surface are weird - they're generally known as great circles. And any two great circles cross in exactly two points. That's not how cartesian lines behave. Worse, the equations for a great circle are not linear when expressed in GPS coordinates, because those are longitude/latitude pairs.
I can imagine that at this point you're feeling a bit confused. You might want to look at this from another side - we have a similar problem with maps. Globe maps are by definition attempts to flatten that non-flat surface. Since that's not exactly possible, you end up with map projections. You can also project the corner points of your polygons on such projections. And because the projections are flat, you can draw the edges on the projection. You now see the problem visually: On two different projections, identical polygons will contain different parts of the world!
So, since we agreed that in the real world, the edges of the polygons are great circles, we should really consider a projection that keeps the great circles straight. There's exactly one family of projections that has this property, and that's the Gnomonic projection. It's a family of projections because you can pick any point as the center.
As it happens, we have one natural point to consider here: the GPS point we're considering. If you put that in the center, draw a gnomonic projection around it, project the polygon edges, and then draw the polygon, you have an exact solution.
Except that the actual earth isn't spherical. Sorry. How exact did you need the test to be, anyway?

Can someone explain how mypoly is used in Python with GPS coordinates?

I understand I can define a polygon in Python using "mypoly".
myPoly = Polygon(p1,p2,p3,…) #list of points
However, what I really want to do is to find a way to use Python to work with polygons that I define with GPS coordinates. The coordinates would show shapes similar to the following:
multiple polygons http://www.jvanderhook.info/images/slabs/drawing_1.png
(the image came from this question on Robotics Stack Exchange
I understand I should be able to work with a polygon once I have it defined, but this is new to me. How can I define one with GPS coordinates? Do I simply replace p1, p2, p3, ... above with the GPS coordinates?
Yes, you can use the gps coordinates to define your polygons. However, understand that these coordinates are typically in degrees (+/- 0-90 lat, +/- 0-180 lon).
Unfortunately, these things aren't as simple as they initially seem they should be. It might help to get comfortable with map projections, here's a good start:
http://kartoweb.itc.nl/geometrics/map%20projections/mappro.html
Then, you may be better off converting to a projection like 900913 (google maps projection) that is in meters and makes points easier to manage.
I tend to use django (which uses geos/gdal/proj4 and is a bit of a pain to get everything working right) for geometry manipulation and transforms. (shapely and pyproj seem like good alternatives if your not using django already)

Cartopy behavior when plotting projected data

I am using cartopy to draw my maps. Its a great tool!
For some of my data I have the problem that the data is not properly mapped around 0deg or the dateline. See the example below.
I know the same feature from matplotlib.basemap, where it can be solved by using the add_cyclic routine. I wondered if somebody can recommend how to best fix this problem in cartopy.
Thanks!
Alex
When plotting global data like this you will always need to add a cyclic point to your input data and coordinate. I don't believe Cartopy currently includes a function to do this for you, but you can do it yourself quite simply for the time being. Assuming you have a 1d array of longitudes and a 2d array of data where the first dimension is latitude and the second is longitude:
import numpy as np
dlon = lons[1] - lons[0]
new_lons = np.concatenate((lons, lons[-1:] + dlon))
new_data = np.concatenate((data, data[:, 0:1]), axis=1)
If you have different shaped data or coordinates then you will need to adjust this to your needs.
The cartopy development team has included the required feature in the developoment branch. For details see here

3D Polygons in Python

As far as I am aware there is no inbuilt polygon functionality for Python. I want to create a 3D map and figured that polygons would be the best way to go about it.
Not wanting to reinvent the wheel I did some googling and found that there's a lot of Python stuff out there, but I couldn't find what I wanted. Thus before I reinvent the wheel (or invent it a whole), does anybody know of a Polygon system for Python?
Note that it does need to be 3D (I found quite a few 2D ones). Note also that I am not interested in the displaying of them but in storing them and the datastructure within Python.
Thanks
One of the most complete geography/mapping systems available for Python that I know about is GeoDjango. This works on top of the Django, an MVC framework. With it comes a large collection of polygon, line and distance calculation tools that can even take into account the curvature of the earth's surface if need be.
With that said, the quickest way I can think of to produce a 3D map is using a height map. Create a two dimensional list of tuples containing (x, y, z) coordinates. Each tuple represents an evenly spaced point on a grid, mapped out by the dimensions of the array. This creates a simple plane along the X and Z axes; the ground plane. The polygons that make up the plane are quads, a polygon with four sides.
Next, to produce the three dimensional height, simply give each point a Y value. This will create peaks and valleys in your ground plane.
How you render this will be up to you, and converting your grid of points into a polygon format that something like OpenGL can understand may take some work, but have a look at Visual Python, its the simplest 3D library I've seen for Python.
I think you mean Polyhedron, not Polygon .. and you might wanna look at vpython
CGAL is a C++ geometry library which, amongst other things, models Polyhedra (3D flat-surfaced shapes)
It has Python bindings available. The documentation for the Polygon3 class is here:
http://cgal-python.gforge.inria.fr/Manual/CGAL.Polyhedron.html#Polyhedron_3

Categories

Resources