Announcement

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

  • spmap code gives me no basemap data error despite data having _ID _CX _CY variables

    Hello,

    My current dataset for each county has _ID _CX _CY variables except for few counties. My main dataset is repeated cross section which I merged with us census county .shp data to collect those particular variables to merge on basis of county code.

    However, when I'm using the following code this gives me error :

    Code:
    spmap hispanic using clean_cps42, id(_ID) clm(custom) clb(0(10)100) fcolor(ln_incwage)
    File clean_cps42.dta is not a valid basemap dataset
    r(198);
    This error means _X is required and is a numeric variable that contains the x-coordinate of the nodes of the base map polygons. _Yis required and is a numeric variable that contains the y-coordinate of the nodes of the base map polygons. I have the data for _X and _Y too for each county but that data has no indicator for what county the _X and _Y are for.


    I'm giving the sample of data below

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(hispanic ln_incwage) double county int _ID double(_CX _CY)
    0  9.685331 1117 26 -86.66066541307332 33.26427760112504
    0 10.567463 1117 26 -86.66066541307332 33.26427760112504
    0         . 1117 26 -86.66066541307332 33.26427760112504
    0         . 1117 26 -86.66066541307332 33.26427760112504
    1  10.52901 1117 26 -86.66066541307332 33.26427760112504
    1  9.424644 1117 26 -86.66066541307332 33.26427760112504
    0 10.304108 1117 26 -86.66066541307332 33.26427760112504
    0         . 1117 26 -86.66066541307332 33.26427760112504
    0 10.276154 1117 26 -86.66066541307332 33.26427760112504
    0 10.723927 10001 964 -75.5684223159937 39.08616892823275
    1  10.34377 10001 964 -75.5684223159937 39.08616892823275
    1  10.27648 10001 964 -75.5684223159937 39.08616892823275
    0   9.64782 10001 964 -75.5684223159937 39.08616892823275
    1         . 10001 964 -75.5684223159937 39.08616892823275
    0    10.272 10001 964 -75.5684223159937 39.08616892823275
    0 10.596635 10001 964 -75.5684223159937 39.08616892823275
    0 10.213102 20045 1655 -95.29259106013197 38.88466799093866
    0         . 20045 1655 -95.29259106013197 38.88466799093866
    0 9.3805895 20045 1655 -95.29259106013197 38.88466799093866
    1  8.582082 20045 1655 -95.29259106013197 38.88466799093866
    1  8.718009 20045 1655 -95.29259106013197 38.88466799093866
    0   9.17295 20045 1655 -95.29259106013197 38.88466799093866
    0 10.309952 20045 1655 -95.29259106013197 38.88466799093866
    0 10.435761 20045 1655 -95.29259106013197 38.88466799093866
    end
    This is the data sample where I have _X and _Y variables for each county :

    [CODE]
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int _ID double(_X _Y) strL rec_header int shape_order
    1                 .                  . "\0\0    H\0\0\0q�::�KV�F��0uB@�Ƽ�84V��]g�B@\0\0\0&\0\0\0\0\0\0"   1
    1 587169.7468999204 3945805.7978895735 ""                                                       2
    1 587291.9675300308  3946568.234408276 ""                                                       3
    1 587542.8784654966 3947603.7875347678 ""                                                       4
    1 587832.8755765569 3948311.3494306263 ""                                                       5
    1 588118.9787921472  3949009.554303093 ""                                                       6
    1 588202.9791910454 3949144.0540915113 ""                                                       7
    1 590315.3146308373  3951371.276341358 ""                                                      13
    11 879967.3559019613  4055356.812745668 ""                                                       2
    11  880042.864661612   4055511.04531594 ""                                                       3
    11 880245.7724900984 4055760.3633284476 ""                                                       4
    11  880236.764172873   4056023.65252873 ""                                                       5
    11 879995.4719701133  4056286.471413132 ""                                                       6
    11 879875.9803839253 4056604.4261883516 ""                                                       7
    11 879928.1582158755  4056908.336943128 ""                                                       8
    11  880115.876620123 4057172.5198362162 ""                                                       9
    11 880170.0840602495 4057438.7541812984 ""                                                      10
    11 880152.9314452348  4057580.624827211 ""                                                      11
    26 836727.6865639449 3553379.6383021716 ""                                                      96
    26 836594.8959286615 3554860.6200469546 ""                                                      97
    26 836591.7283392546  3554941.186571909 ""                                                      98
    26 836585.3291683497 3555008.8684600256 ""                                                      99
    26 836567.0404264085 3555233.6770346966 ""                                                     100
    end
    Can I merge the data where I have _X and _Y variable with the data where I have county, _ID, _CX and _CY variables ?? The data with county, _ID, _CX and _CY variables is my master data
    Last edited by Tariq Abdullah; 01 Oct 2022, 16:23.

  • #2
    I believe #2 in this thread will solve your problem. The issue appears to be the lat/lon in your polygon file. See x1 and y1 below.

    Code:
    clear
    input int _ID double(_X _Y) strL rec_header int shape_order
    1                 .                  . "\0\0    H\0\0\0q�::�KV�F��0uB@�Ƽ�84V��]g�B@\0\0\0&\0\0\0\0\0\0"   1
    1 587169.7468999204 3945805.7978895735 ""                                                       2
    1 587291.9675300308  3946568.234408276 ""                                                       3
    1 587542.8784654966 3947603.7875347678 ""                                                       4
    1 587832.8755765569 3948311.3494306263 ""                                                       5
    1 588118.9787921472  3949009.554303093 ""                                                       6
    1 588202.9791910454 3949144.0540915113 ""                                                       7
    1 590315.3146308373  3951371.276341358 ""                                                      13
    11 879967.3559019613  4055356.812745668 ""                                                       2
    11  880042.864661612   4055511.04531594 ""                                                       3
    11 880245.7724900984 4055760.3633284476 ""                                                       4
    11  880236.764172873   4056023.65252873 ""                                                       5
    11 879995.4719701133  4056286.471413132 ""                                                       6
    11 879875.9803839253 4056604.4261883516 ""                                                       7
    11 879928.1582158755  4056908.336943128 ""                                                       8
    11  880115.876620123 4057172.5198362162 ""                                                       9
    11 880170.0840602495 4057438.7541812984 ""                                                      10
    11 880152.9314452348  4057580.624827211 ""                                                      11
    26 836727.6865639449 3553379.6383021716 ""                                                      96
    26 836594.8959286615 3554860.6200469546 ""                                                      97
    26 836591.7283392546  3554941.186571909 ""                                                      98
    26 836585.3291683497 3555008.8684600256 ""                                                      99
    26 836567.0404264085 3555233.6770346966 ""                                                     100
    end
    
    python:
    from sfi import Data
    x = Data.get('_X')
    y  = Data.get('_Y')
    
    import pyproj
    NYSP1983 = pyproj.Proj("ESRI:102718")
    x1, y1 = NYSP1983(x, y, inverse=True)
    
    Data.addVarDouble("x1")
    Data.addVarDouble("y1")
    Data.store("x1", None, x1)
    Data.store("y1", None, y1)
    end
    replace x1 = . if _X ==.
    replace y1 = . if _Y == .
    list _ID _X _Y x1 y1
    
         +------------------------------------------------------+
         | _ID          _X          _Y           x1          y1 |
         |------------------------------------------------------|
      1. |   1           .           .            .           . |
      2. |   1   587169.75   3945805.8   -75.693426   50.922926 |
      3. |   1   587291.97   3946568.2   -75.692968   50.924987 |
      4. |   1   587542.88   3947603.8   -75.691984    50.92779 |
      5. |   1   587832.88   3948311.3   -75.690805   50.929712 |
         |------------------------------------------------------|
      6. |   1   588118.98   3949009.6   -75.689643   50.931609 |
      7. |   1   588202.98   3949144.1   -75.689296   50.931976 |
      8. |   1   590315.31   3951371.3    -75.68047   50.938087 |
      9. |  11   879967.36   4055356.8   -74.447171   51.227665 |
     10. |  11   880042.86     4055511    -74.44685   51.228082 |
         |------------------------------------------------------|
     11. |  11   880245.77   4055760.4   -74.445986   51.228756 |
     12. |  11   880236.76   4056023.7    -74.44603   51.229464 |
     13. |  11   879995.47   4056286.5    -74.44707   51.230169 |
     14. |  11   879875.98   4056604.4    -74.44759   51.231023 |
     15. |  11   879928.16   4056908.3   -74.447373   51.231842 |
         |------------------------------------------------------|
     16. |  11   880115.88   4057172.5   -74.446573   51.232556 |
     17. |  11   880170.08   4057438.8   -74.446347   51.233273 |
     18. |  11   880152.93   4057580.6   -74.446423   51.233655 |
     19. |  26   836727.69   3553379.6   -74.617408   49.872262 |
     20. |  26    836594.9   3554860.6   -74.618008   49.876265 |
         |------------------------------------------------------|
     21. |  26   836591.73   3554941.2   -74.618024   49.876483 |
     22. |  26   836585.33   3555008.9   -74.618052   49.876666 |
     23. |  26   836567.04   3555233.7   -74.618135   49.877273 |
         +------------------------------------------------------+

    Comment


    • #3
      Dear Mr. Niakamal,

      I have tried several times installing pyproj after installing python through stata. However, it's returning me there is nothing name pyroj when I'm executing the command import pyproj. In the following public census website the county .shp data is given.

      https://www.census.gov/geographies/m...5CHAZKP3006NN9

      cb_2018_us_county_500k.zip [11 MB]

      After downloading, you just need the following code to get the data

      Code:
      spshape2dta cb_2018_us_county_500k.shp, saving(usacounties) replace
      
      use usacounties_shp.dta, clear
      
      list _ID _X _Y shape_order in 1/10, abbreviate(11)
      You' ll get the data I have. Would you be kind enough to process the same command with this data , and attach me here.

      Downloading python at this point on my laptop will take up some space and arrangement which at this point seems a little overwhelming given that I'm doing a several stata projects for which I need the space. On top of that, I'm not sure how to use python through stata either.

      If you are just kind enough to transform the data, then I can download it from here. If only if your time allows it.


      Last edited by Tariq Abdullah; 02 Oct 2022, 12:36.

      Comment


      • #4
        This should give you an idea. It's not clear what you want to map, given the example data. You'll need a unique entry for each _ID.


        Here's an example using that shapefile

        Code:
        clear all 
        
        
        copy "https://www2.census.gov/geo/tiger/GENZ2018/shp/cb_2018_us_county_500k.zip" "cb_2018_us_county_500k.zip"
        unzipfile cb_2018_us_county_500k.zip
        
        spshape2dta  cb_2018_us_county_500k
        
        
        clear
        input float(hispanic ln_incwage) double county int _ID double(_CX _CY)
        0  9.685331 1117 26 -86.66066541307332 33.26427760112504
        0 10.567463 1117 26 -86.66066541307332 33.26427760112504
        0         . 1117 26 -86.66066541307332 33.26427760112504
        0         . 1117 26 -86.66066541307332 33.26427760112504
        1  10.52901 1117 26 -86.66066541307332 33.26427760112504
        1  9.424644 1117 26 -86.66066541307332 33.26427760112504
        0 10.304108 1117 26 -86.66066541307332 33.26427760112504
        0         . 1117 26 -86.66066541307332 33.26427760112504
        0 10.276154 1117 26 -86.66066541307332 33.26427760112504
        0 10.723927 10001 964 -75.5684223159937 39.08616892823275
        1  10.34377 10001 964 -75.5684223159937 39.08616892823275
        1  10.27648 10001 964 -75.5684223159937 39.08616892823275
        0   9.64782 10001 964 -75.5684223159937 39.08616892823275
        1         . 10001 964 -75.5684223159937 39.08616892823275
        0    10.272 10001 964 -75.5684223159937 39.08616892823275
        0 10.596635 10001 964 -75.5684223159937 39.08616892823275
        0 10.213102 20045 1655 -95.29259106013197 38.88466799093866
        0         . 20045 1655 -95.29259106013197 38.88466799093866
        0 9.3805895 20045 1655 -95.29259106013197 38.88466799093866
        1  8.582082 20045 1655 -95.29259106013197 38.88466799093866
        1  8.718009 20045 1655 -95.29259106013197 38.88466799093866
        0   9.17295 20045 1655 -95.29259106013197 38.88466799093866
        0 10.309952 20045 1655 -95.29259106013197 38.88466799093866
        0 10.435761 20045 1655 -95.29259106013197 38.88466799093866
        end
        
        
        contract _ID 
        spmap _freq using "cb_2018_us_county_500k_shp.dta", id(_ID)

        Comment


        • #5
          That was a great workaround of the problem I was having! Needless to say Mr. Niakamal that you have been very kind and thorough with your time and suggestion regarding a rather unconventional issue with stata! Cannot thank you enough for suggesting an efficient method of getting this thing done! Truly obliged!

          Comment

          Working...
          X