Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • convert county FIPS to centroid coordinates

    Dear Users,
    I want to convert a set of US county FIPS to centroid coordinates and draw a map of these points. Could you please guide me how to do this? Thank you.

    Code:
    input countryFIPS
    53033
    6007
    6089
    53071
    6103
    1073
    end

  • #2
    Hi Lynn,

    You might be able to find a csv file with country FIPS codes and centroid coordinates and merge that file with your data. This should be simple and straightforward provided you can find reliable data.

    Otherwise, you might consider using GIS software like ArcGIS or the open source alternative QGIS, (or geopandas if you are a python user), get a shape file, and calculate the centroid of each polygon yourself. Stata appears to have some support for working with shapefiles (see -help sp-), but I'm honestly not sure if it is the most appropriate tool for the job. Maybe someone more knowledgeable than myself can jump in.

    Edit: you may be able to draw the map with Stata's built in shape file support and an appropriate shape file. Again, I would strongly prefer ArcGIS for this, but your preferences may differ.
    Last edited by Daniel Schaefer; 27 Sep 2022, 09:20.

    Comment


    • #3
      Yeah never use ARCGIS, Stata can read shapefiles happyily..


      In fact, whenever I make panels from administrative data, i always use the shapefile, just in case I wanna make a map. Believe you me, you wanna do all your stuff in one place, that way, everything is replicable assuming everyone has the same technology/dependencies

      Comment


      • #4
        Thank you Daniel and Jared for your help. I prefer to do it all in Stata. Can anyone please guide me how to convert US county FIPS to centroid coordinates? Many thanks.

        Comment


        • #5
          I promise, when I get home I'll show you how I'd do this. I'll give a full reproducible example.

          Comment


          • #6
            Hi Lynn,

            It looks like you may need a third party library to calculate centroids in Stata. You might start with this thread: https://www.statalist.org/forums/for...gon-with-spmap

            Comment


            • #7
              Yeah never use ARCGIS, Stata can read shapefiles happyily..
              ArcGIS is the industry standard in GIS software. Stata can read shapefiles, but it doesn't have anything close to the support for GIS that ArcGIS has. I can see why one might prefer to use a single platform, but "never use ArcGIS" strikes me as rather extreme. Moreover, I routinely use multiple platforms and I regularly share code with colleagues who do the same. This does not pose a problem for "replicability."

              Comment


              • #8
                I agree with Daniel, so let me offer a massive qualifier: I said never use ArcGIS... but I don't really mean that. What I mean to say, is if you need to do some wild stuff like changing coordinate projections (which Stata can't do, unfortunately), or some really sexy (complicated) mapping work.... then by all means, use ArcGIS. Or Python, Python is also a godsend (even though I don't believe in God) in this department, too.

                What I really mean, is that (and I don't know your field or occupation), in MOST applications that you're LIKELY to face, ArcGIS likely won't strictly be necessary for whatever your purposes are. This doesn't mean don't use it or that it's bad, but it isn't necessary for your ends. My point about replicability is a little different: I know some folks use different softwares , and that's great. I use Stata and Python... but not everyone does. So, if you are sharing code to those who only know Stata, I prefer to do all the stuff (that I can do!) in Stata to minimize someone not being able to work with my stuff due to lack of Python knowledge. But of course, this is completely context dependent, especially since Stata can use Python directly from do files now, so as long as you're organized, by all means, use both, nothing wrong with it at all. Okay rambling over, Here is the code that does as you wish.
                Code:
                loc zf cb_2018_us_county_500k
                
                
                copy "https://www2.census.gov/geo/tiger/GENZ2018/shp/`zf'.zip" "`zf'.zip", replace
                
                
                
                unzipfile `zf'.zip, replace
                
                erase `zf'.zip
                
                * Makes Stata format shapefile
                spshape2dta `zf'.dbf, replace saving(us_final)
                
                foreach name in cb_* {
                
                ** Grabs the shapefile data we're interested in
                local files : dir "`c(pwd)'" files "*`name'*"
                
                foreach f of loc files { // moves these back to the raw data directory
                    
                erase  `f'
                }
                }
                
                
                u us_final, clear
                So let's say we wanna map where I live
                Code:
                grmap if _ID == 3089
                Now say we wanna map where I live and the bordering county
                Code:
                grmap if inlist(_ID,3089,2072)
                your example (if I understand your data well) would be
                Code:
                u us_final, clear
                cls
                destring STATEFP COUNTYFP, replace
                
                sort STATEFP COUNTYFP 
                
                grmap if (STATEFP == 6 & inlist(COUNTYFP,7,89,103)) | inlist(_ID,1074,618,1037)
                This makes me ask........ is this the graph you desire? Why exactly these places? The map looks kinda awkward, so I was just curious.

                Comment


                • #9
                  Hi Daniel and Jared, thank you very much for your time and the discussion.
                  Many thanks to Jared for the code. I will try to replicate them and let you know.

                  Comment


                  • #10
                    Dear Users and Jared,
                    On a follow-up, I have already be able to prepare the point map of firm location (headquarters and subsidiaries) using the code below and need your help with two questions. (1) how could I present firm headquarters and subsidiaries differently on the map, say triangle for headquarters and round dots for subsidiaries? (2) Why is the map so small and how could I make it larger? Thank you very much for your time and help. Lynn.

                    Code:
                    clear
                    
                    input clon clat
                    
                    -86.82805 33.527872
                    -86.260844 32.331872
                    -149.7621 61.1919
                    -147.56838 64.81859
                    -112.08791 33.494514
                    -114.08271 35.285985
                    -92.294471 34.766541
                    
                    end
                    
                    rename clon _X
                    
                    rename clat _Y
                    
                    save location, replace
                    
                    /*******/
                    
                    copy "https://www2.census.gov/geo/tiger/GENZ2018/shp/cb_2018_us_county_500k.zip" "cb_2018_us_county_500k.zip", replace
                    
                    unzipfile "cb_2018_us_county_500k.zip", replace
                    
                    shp2dta using cb_2018_us_county_500k, data("us-attr.dta") coord("us-coord.dta") genid(id) replace
                    
                    use "us-attr.dta", clear
                    
                    spmap using "us-coord.dta" if STATEFP!="15" & STATEFP!="60" & STATEFP!="66" & STATEFP!="69" & STATEFP!="72" & STATEFP!="78", id(id) point(data("location.dta") x(_X) y(_Y) shape(triangle) size(*0.5) fcolor(red) ocolor(white) osize(vvthin))
                    
                    graph save Graph.gph, replace

                    Comment

                    Working...
                    X