How should I slice geometries in a Json file with Geopandas? - python

My problem is that I can't find a code to automate the generation of another json as an output file to cut and join polygons, this Json file has dictionaries and within these it has coordinates (X,Y).
With the help of Geopandas I was trying to find a code that would cut the overlapping polygons, and with the other non-overlapping polygon just merge with the last polygon, i.e. first find the base of the overlapping polygon, then cut these overlapping ones and with the other non-overlapping polygon just join them, I know I should use geopandas overlap method, with how="union" and "difference".

Related

Sub-sample shapefile coordinates of shapely polygon

I have a standard shapefile which has a column called geometry. This column contains a shapely Polygon object which is essentially a list of paired latitudes and longitudes that trace an object (e.g. the border of a country). The shapes are very complex/detailed, meaning that they have many points and are thus hard to plot. Is there a way to intelligently sub-sample the points in each polygon (e.g. taking only 1% of the points) in python.
I use the word intelligently because I need to ensure a few things:
The first and last point in the polygon must be the same (so that the shape reconnects to itself
If I have two polygons that share a points (e.g. when two countries share a border), there should be minimal overlap between the boundaries after sub-sampling.
An example of a piece of software that does this is https://mapshaper.org/. I want to do the same thing as their "simplify" tool, but in python.

Could anyone please show me how to correctly use GDAL Grid in Python?

I am trying to create a group of contour polygons from a set of scatter points containing numeric data of some sorts, for example temperature.
The method I used to apply is to generate a raster through spatial interpolation then perform contour then construct polygons with these contour lines or convert raster cells to polygons. All these were done in ArcGIS.
But considering the expiration of license, I want to do the same thing in plain python. I tried PostGIS but it did not work out. Now I turn to GDAL.
My plan is to create a smooth surface geotiff file from points with gdal.Grid, then use gdal.ContourGenerate to get contour lines, and finally construct polygons with these contour lines and some mask line rings such as a border. However I am stuck at the very first step.
This is my code, the script only creates empty geotiff file, the documentation of GDAL does not really tell much on this function. Could anyone help me solve this?
the input test.shp is a ESRI shapefile, it has a column named 'value' which stores the data I want to use for interpolation.
import gdal
option = gdal.GridOptions(format='GTiff',
width=250,height=250,
algorithm='invdist:power=2',
layers=['d:/work/contour/test.shp'],
zfield='value')
out = gdal.Grid('d:/work/contour/test.tif',
'd:/work/contour/test.shp',
options=option)
out.FlushCache()
out = None
del out

python shapely intersection function: clock-wise or counter-clock-wise

I have used the Shapely polygon intersection function:
object.intersection(other)
and get inconsistency in the direction and order of the vertices of the output polygon.
Is there a way to have a systematic set of outputs, or should I run through the output polygon and sort it?
You may get better answers on https://gis.stackexchange.com/
Double check that you are using the right DE-9IM method. DE-9IM Wikipedia
If I understand you correctly with the systematic outputs you have multiple LinearRings inside your polygon and you want a separate result for each vs just having a single Boolean result for the entire polygon intersecting with other. The easiest way which is slower is to iterate through your polygon and compare each LinearRing. The faster way is to use sr-trees with the python package "rtrees". https://gis.stackexchange.com/a/119935/60045

python shapely plot multiple layers

There is a set of (lat, long) points which can be simply visualized by matplotlib.pyplot.scatter(list_of_longitudes, list_of_latitudes). But I want to plot these points on top of a road network which happens to be in a shapefile. How can I simply plot the two files together with Python in one single map/figure?
Especially looking for something like Shapely

Generate heatmap image of lines

I have this graph, which contains a lot of lines defined by 2 points. Now I would like to generate a heatmap. The result should be something similar to http://docs.ggplot2.org/current/geom_raster.html, except the heat of each cell is defined as how many lines intersect it. The result should be a numpy array for PIL.
I considered two things so far, one would be to iterate over all lines and grids and calculate the distance from the two with this and if the distance is smaller than between two cells, it's in there. This is possible because in the actual data, the lines have a certain minimum length.
Another possibility to intersect each line with each grid cell and check if it's in there. But that sounds expensive, but more accurate. Along the lines that you check each horizontal/vertical line for intersection and check in which cell it is.
Data sample. It's an Array[Array[Int, Int]]. Every pair of sub-arrays designates a line.
This sounds like what you want:
How to test if a line segment intersects an axis-aligned rectange in 2D?
In particular the top answer: https://stackoverflow.com/a/293052/66349
In summary:
check if both points of the line are to one side of the box (left, right, above, below) - if so, there is no intersection
otherwise, check if all four corners of the box are on the same side of the line - if they are not, then you have an intersection
Generally speaking, it might be faster to iterate over the line segments and test only a subset of heat map boxes (perhaps boxes that fall into the larger box defined by the two points of the line); rather than iterating over the heat map boxes and checking every line for intersections.

Categories

Resources