GeoDjango Querying location__within=poly uses Boundaries instead of Actual Shape - python

I'm using GeoDjango to find all points within a Polygon but it seems to be using the Boundaries (NMW,NME,SME,SMW) to find points. So it brings back results that are outside of the primary shape.
polygon = Polygon((18.3825363358424 -33.97219070578159,...))
Order.objects.filter(location__within=polygon)
I would like the query to bring points inside the shape and not inside its bounds.
i.e if * was my shape; I'm getting points in # that seems to suggest that it's querying on the bounds instead of the actual shape.
**************
**************
******########
******########
******########
Please tell if I'm doing some wrong?

Ok looks like this is a MySQL Limitations
https://docs.djangoproject.com/en/2.2/ref/contrib/gis/db-api/#mysql-spatial-limitations
In other words, while spatial lookups such as contains are available in GeoDjango when using MySQL, the results returned are really equivalent to what would be returned when using bbcontains on a different spatial backend.
:( any other way of solving this problem

Related

Keep lat long order using transformer.from_crs, always_xy=True not working?

I'm trying to update some code because lat/lon values are getting flipped (lat ends up in lon column and vice versa) when using Transformer.from_crs.
The line of code that is causing the issue is this, BUT only when f'epsg:{oldEPSG}'="epsg:4326" (so only when we are converting from AND to epsg:4326).
transformer = Transformer.from_crs(f'epsg:{oldEPSG}', "epsg:4326")
I have tried adding the 3rd argument always_xy=True, as suggested in https://github.com/pyproj4/pyproj/issues/510, but get the same result.
We have coordinates with epsg:6491, epsg:26919 and epsg:5646 in our database which don't have their lat/lon flipped after transforming.
I'm not really familiar with working with coordinate, so I'm confused if I need to be figuring out if they have default lat/lon or lon/lat orders (like will epsg:4326, epsg:6491, epsg:26919 and epsg:5646 all be different?).
I guess I'm just wondering why the lat/lon don't get flipped in all cases? If it's just a matter of checking whether the from and to projections are the same, I can do that. I just want to make sure I understand why this is happening in case other projections end up having the same issue.
Thank you!

Creating Regions in Python

I am trying to use Python to create a simple solution.
I have latitude and longitude of various areas, alongside their ID number and a code. The code is used to define regions. Say three locations (near to each other) have the same code, so they form region_1 and so on. I am trying to show this on map (basically show different regions) but don't understand how to approach the problem.
I tried using Folium Choropleth but it didn't work.
I think that is because I am not looking for how a value varies across regions. I am just interested in seeing how I can use different points to represent a region on a map.
Any help to what I can look into would be appreciated!
Edit: So turns out I have to make use of Voronoi regions.
Now I tried creating those regions in Python.
Firstly I got the area shape using:
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
area = world[world.name == 'Pakistan']
area = area.to_crs(epsg=3395) # convert to World Mercator CRS
area_shape = area.iloc[0].geometry # get the Polygon
This gave the correct output. Next I had two lists, lat and long.
I combined the two lists into a 2D array and passed it to:
from geovoronoi import voronoi_regions_from_coords
region_polys, region_pts = voronoi_regions_from_coords(arr, area_shape)
This is the error I am getting now:
RuntimeError: ridge line must intersect with surrounding geometry from `geom`; this error often arises when there are points outside of the surrounding geometries; first check if all your points are inside the surrounding geometries
I googled a bit but don't know how fix the error.
The points are fine I think since I did map it in Mapinfo first.
Would appreciate any help on this!

Finding attributes near route (linestring)

Basically, I want to know if there is a way to query all objects with a geographic field that are near a linestring in django using postgis. I am calculating a route and want to find objects that are approximately on that route (say max distance from the linestring 2m - 10m). I cannot find a way online that seems to solve this.
I can, of course, create an interval around the route of say 2m, and then create a polygon using these intervals and check which points fall within this created surface, but I wonder if there is a more direct approach (the way I described above).
This is some psu-code for my first described method (this i want)
def get_objects_on_route():
Model.objects.filter(geo_location__some_lookup=all_points_in_route_route, max_distance=2m)
other method which I could implement
def get_objects_on_route():
points_in_surface = []
for each element in route:
points_in_surface.append(two_corrected_points)
poly= Polygon(all_points_on_route)
Model.objects.filter(geo_location__covered_by=poly)
As it turns out, you can easily make this kind of query using Django's PostGIS filtering by creating a LineString object instead of a Point.
from django.contrib.gis.geos import LineString
line = LineString(*coords_in_route)
Model.object.filter(geo_location__distance_lte=(line, D(m=2)))
I tested it and seems to be working correctly:

django-rest-framework-gis GeoFeatureModelSerializer returning altered/incorrect coordinates upon serialization

Currently, I have a GeometryField, which holds a Polygon, which is a GEOSGeometry. I print the coordinates of the polygon, and they seem fine, right where I specified. Then, I save the instance of the model, and then deserialize with the GeoFeatureModelSerializer, only to find out that my polygon's coordinates have been changed to something very small and close to the equator.
This is the GEOSGeometry stored in the GeometryField initially that gets stored in the database.
POLYGON ((-79.94751781225206 40.44287206073545,
-79.94751781225206 40.44385187931003,
-79.94502872228624 40.44385187931003,
-79.94502872228624 40.44287206073545,
-79.94751781225206 40.44287206073545))
This is after that is serialized with the GeoFeatureModelSerializer and returned.
[[-0.000718176362453, 0.000363293553554],
[-0.000718176362453, 0.000363316438548],
[-0.000718135112337, 0.000363316438548],
[-0.000718135112337, 0.000363293553554],
[-0.000718176362453, 0.000363293553554]]
I have no idea what could be causing this.
Thanks a lot in advance.
This was resolved by specifying the SRID. According to the Django docs, the SRID is
Choosing an appropriate SRID for your model is an important decision that the developer should consider carefully. The SRID is an integer specifier that corresponds to the projection system that will be used to interpret the data in the spatial database. (https://docs.djangoproject.com/en/2.0/ref/contrib/gis/model-api/)
I performing operations on polygons with a particular SRID and returning another polygon with a different SRID. I simply had to 'cast' the polygon I was returning to the SRID I wanted, with GEOSGeometry(polygon, srid=some_value). Basically, the polygon I was returning was being projected to some other format that I didn't want.

Convert Point Database to Body Shape

I have a database containing body scan results in point format.
For example :
point1=(x,y,z)
point2=(x2,y2,z2)
...
I want to convert these points to body shape.
And I want to do some processing on this points for example calculate neck diameter and some related calculation.
Any suggestion ? (module , tutorial etc...)
You need some basic textbook on computational geometry. See this question, for example: https://stackoverflow.com/questions/3308266/computational-geometry

Categories