Coder Social home page Coder Social logo

Comments (7)

micahcochran avatar micahcochran commented on August 27, 2024

I like the idea of a few lines of code detecting if the projections are the same. I am unsure if the built-in transform() should do that checking.

Your work around will work for the above case because you defined both projections in exactly the same way. It is not a bad way to test. Look at my example using the same projection defined in two different ways.

p1 = pyproj.Proj(init='epsg:26915')
p2 = pyproj.Proj('+proj=utm +zone=15 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0')
if p1.srs == p2.srs:
  print("They are equal???")     # this shouldn't happen
else:
  print("Test fails.")

In order to make a projection equivalence test, PyProj would either have to expose pj_get_def() from Proj.4 or create a new function Proj.__eq__() that to check if the projections are equivalent and most likely use the `pr_get_def() .

I like the idea of a Proj.__eq__() so you can test p1 == p2. Then, your code could test == and skip the transform. There other useful application for that projection equivalency of function.

from pyproj.

fmaussion avatar fmaussion commented on August 27, 2024

Yes I see. I would find a Proj.__eq__() very useful in general, yes! How complicated would it be to implement?

from pyproj.

micahcochran avatar micahcochran commented on August 27, 2024

Pseudocode of how I would make a Proj.__eq__().

  1. Run pj_get_def() on a projection
  2. convert Proj4 string to Fiona.crs dictionary representation, from_string()
  3. remove those key from dictionary that are for metadata, such as the 'init' key
  4. Do steps 1-3 on a 2nd projection.
  5. compare parameters that a functionally equivalent or convert them, such as '+to_meters=.3048' and '+units=ft'
  6. return Boolean value of p1_dict == p2_dict

This seems fairly easy to me.

Below are a few more notes:
Running pj_get_def() for the projection string '+init=epsg:26915'
returns the string ' +init=epsg:26915 +proj=utm +zone=15 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0'

Fiona has crs.py, which has the from_string(), which converts a above Proj.4 strings into dictionaries. If the licenses will allow, I would just copy from_string() into pyproj.

I don't know Cython, which pyproj uses to interface with the Proj.4, but Cython seems fairly straightforward.

from pyproj.

micahcochran avatar micahcochran commented on August 27, 2024

Just to add one more thing. The goal of this code is very similar to the already existing OGR function IsSame(), (C++ code) which can be accessed in Python through osgeo.osr library.

If you need that type of functionality right now, I'd use that library. If you are already using Fiona, using osr shouldn't be any more of a burden to your users. PyProj probably should not be relying upon OGR.

from pyproj.

fmaussion avatar fmaussion commented on August 27, 2024

@micahcochran sorry it took me so long to get back to you. Indeed your suggestion is working perfectly (see test case below). I agree with you that pyproj should avoid depending upon OGR (the fact that pyproj is lightweight is really great). I just changed to the title and keep the issue open for later use. A "homemade" p1.is_same(p2) would still be very useful.

EDIT: or even more pythonic: p1.__eq__(other).

IsSame using OGR:

import pyproj
from osgeo import osr 

# Pyproj version
p1 = pyproj.Proj(init='epsg:26915')
p2 = pyproj.Proj('+proj=utm +zone=15 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0')
if p1.srs == p2.srs:
      print("They are equal")
else:
      print("Test fails.")  # This is what happens

# OGR version
s1 = osr.SpatialReference()
s1.ImportFromProj4(p1.srs)
s2 = osr.SpatialReference()
s2.ImportFromProj4(p2.srs)
if s1.IsSame(s2):
      print("They are equal")   # This is what happens
else:
      print("Test fails.")

from pyproj.

micahcochran avatar micahcochran commented on August 27, 2024

No problem @fmaussion

In my limited additional testing and OGR/GDAL seems to fail sometimes, but that does work better for more cases than p1.srs == p2.srs.

This would be extending pyproj beyond the capabilities of proj.4's public facing API, which pyproj mirrors fairly closely. OGR/GDAL depends upon proj.4 for its coordinate transformations, but extends its capabilities with the IsSame and other functions.

I'm going to make a suggestion that's not quite going to solve your problem. PyCRS is a python library has some of the workings to understand projection parameters. It is already a useful library to convert between WKT and proj4 projection representations. It seems like a projection equality comparison could compliment it. It might not be too hard extend it. Note: PyCRS still has a few rough edges.

from pyproj.

fmaussion avatar fmaussion commented on August 27, 2024

Thanks a lot @micahcochran ! I'll have a look at PyCRS, although I think that the simple solution above will largely fulfill my needs.

I'll close this issue then.

from pyproj.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.