Reproject with GeoAccessor project method.

623
8
Jump to solution
04-22-2021 09:11 AM
Labels (1)
BrianWilson7
Occasional Contributor III

I am using a Notebook in ArcPro 2.7 to process some data using GeoAccessor dataframes. Works great until I try to reproject the df before the final step, writing it to feature classes.

I worked beyond annoying ERROR 9999 messages, by passing in a SpatialReference object instead of a WKT string. 

I get no indication of errors now! Unfortunately nothing happens, the "project" method is supposed to do an "inplace" conversion but the conversion does not happen. The method returns "False" but there is no documentation on whether that's good or bad or what happened.

screenshot.PNG

0 Kudos
1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

If you have access to arcpy, why would you want to use another packages projections method?

An "in place" projection would entail deleting existing geometry and replacing it with new geometry.  Is there a reason you would want this.


... sort of retired...

View solution in original post

8 Replies
BrianWilson7
Occasional Contributor III

I figured out that I can pass an integer as the WKID into project instead of a SpatialReference.

Also I realized I have to use the copy method on dataframes else I am only creating reference and not a copy of the data. I think it should look like this, the first one works and the second is grinding along; I think the transformation string might be wrong but no feedback, just processor cycles burning. I'll go for coffee, a a watched GIS script never boils.

 

wm_df = df1.copy()
wm_df.spatial.project(3857)
local_df = df1.copy()
local_df.spatial.project(2913, 'NAD_1983_HARN_To_WGS_1984_2')

 

0 Kudos
BrianWilson7
Occasional Contributor III

I ran the same code without the transformation string and it finishes but of course, the results are off. Not surprised. I have to know what magic string to use for the transformation, it is undocumented. I pulled that one from the Project tool in Arc. For now I will use arcpy, save the DF to a feature class in WGS84 and then run the arcpy version.

0 Kudos
DanPatterson
MVP Esteemed Contributor

If you have access to arcpy, why would you want to use another packages projections method?

An "in place" projection would entail deleting existing geometry and replacing it with new geometry.  Is there a reason you would want this.


... sort of retired...
BrianWilson7
Occasional Contributor III

Well, Dan, since you asked, I will go off topic and gush about Pandas and dataframes.

I got in the habit of working with Pandas dataframes doing tabular data processing. (COVID dashboards) Dataframes are fast, all in memory, convenient. You have to get beyond the linear / iterative processing mindset but once you do, WOW. It's pythonic-- they generally just do what I expect without pain.

I have not been a fan of notebooks but they are starting to grow on me. I generally have always written and run Python in an IDE; it's far superior for debugging as I can run one line of code at a time. Using VS Code I can have two panels open side by side, I put the pure python source code in one and open a Jupyter session in the other. Then I can keep refining the complete script and keep it in github source control and also try out ideas in the notebook panel.  BTW visualizing maps works in there too, that blew me away.

This is the first time I have tried using a Notebook in ArcPro. My idea was to make some tools for normal GIS folk to run, so I want it to be comfortable for them not force them into VSCode. I think it will be great that way even though there is no easy way to put it into source control.

Dataframes and Jupyter just go together so nicely. You load a feature class into a dataframe and then it sits there patiently in memory waiting while you muck about with it. 

Regarding inplace -- that was Esri's decision. Most methods have an "inplace=True" option including the Geopandas reproject. They left that out in this version of Esri "project" and just overwrite. It's not a big deal because it adds one line of code, "new_df = old_df.copy()" as mentioned above. Sometimes I prefer "inplace" other times it's better to create a new df so that you have access to both datasets in memory and they both patiently wait for you to use them. ;)  

I've always been annoyed at the Esri model of handling data: first, make a copy of your data, then do step one which generates a copy, then do step 2 which generates... another copy... etc etc when you're done, go back and delete all those intermediate files. Oops! Start over.  How fast is your hard drive, great you've got an NVME SSD! But keeping it all in memory is still faster. If I create new dataframes instead of "inplace=True" then my "intermediate files" are still all in memory too, so I can back up one step and try again when I make baby steps learning a new process and try out different approaches. 

With dataframes, it's all in memory and you decide (often) if you want to make a new df or overwrite the existing one.

Does it work with terabytes of raster data? So far, I don't care, because I work in a small county so every byte of vector and table that I am working with fits in memory on my desktop. Also in 3 projections. Still fits. 

I am banking on Esri continuing to improve the ArcGIS python modules, and that GeoAccessor code and docs will get better because it's such a great way to do GIS.

I also think if they provide us with two different ways to call the projection code they should BOTH WORK. I should not have to know which one works and which one is broken.   :) But I am also totally pragmatic and will use what works and come back to the half-finished Geoaccessor method later. 

0 Kudos
jcarlson
MVP Frequent Contributor

Where's the original dataframe coming from? There are ways to get the spatial reference set to something else when the dataframe is originally instantiated, depending on what the preceding cells of your notebook look like.

- Josh Carlson
Kendall County GIS
BrianWilson7
Occasional Contributor III

The original data is in a table with lat/lon columns. I pull that into a dataframe and then convert the columns into geometry. I might be able to reproject then too but my desired output will be in both WM for web and the local projection (with transformation) for an enterprise GDB so I still have to project it.

This time out I am taking Dan's advice (almost always the right path) and using the arcpy reproject and in a few months when the data is updated and we're at ArcPro 2.8 I will try project() method again.

I bet it would work today if I know what string to put in the transform parameter but I've had enough for today.

 

0 Kudos
jcarlson
MVP Frequent Contributor

The GeoAccessor stuff can be a bit wonky, but it's great when it works.

I prefer using GeoPandas myself when the situation allows, and then using GeoAccessor.from_geodataframe if I truly need to bring it into the ArcGIS Python API. Reprojecting (and a few other things) tends to be a lot simpler in GeoPandas.

- Josh Carlson
Kendall County GIS
DanPatterson
MVP Esteemed Contributor

If one loves Pandas and related stuff... learn its numpy roots... something I have been advocating in my blogs for years.


... sort of retired...