Announcement

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

  • Struggling to create x and y coordinates from my shapefile using geo2xy

    I am not a expert user in stata but working on a coordinates dataset for district boundaries in Pakistan and trying to create x and y cartesian coordinates for point/labels in those districts. Purpose of this exercise to develop choropleth maps of districts and labels on districts. I can create choropleth maps but unable to label districts because my dataset dont have x and y coordinates for district centers.



    use "uscoord.dta", clear
    geo2xy _Y _X, projection( mercator) replace
    local lon0 `r(lon0)'
    local f `r(f)'
    local a `r(a)'
    save "uscoord_mercator.dta", replace

    use usdb,clear
    gen x = runiform()
    save "usdb2",replace

    gen labtype = 1
    append using usdb2
    replace labtype = 2 if labtype==.
    replace ADM2_EN = string(_ID, "%3.0f") if labtype ==2
    gen x_c=runiform()
    gen y_c=runiform()

    keep x_c y_c ADM2_EN labtype
    geo2xy y_c x_c, projection( mercator, `a' `f' `lon0' ) replace
    save maplabels, replace

    clear
    import excel "corana_cases.xlsx", sheet("District") firstrow
    merge 1:1 _ID using usdb2
    drop if _merge!=3

    spmap cases using uscoord, id(_ID) fcolor(BuRd) clmethod(custom) clbreaks(1 10 20 30 40 500) ndlabel("No Cases") legend(position(5)) title("District Wise Confirmed COVID-19 Cases") subtitle("26 March 2020") ///
    label(data(maplabels) xcoord(x_c) ycoord(y_c) ///
    label(ADM2_EN) by(labtype) size(*0.85 ..) pos(12 0) )

    But when i run this code, x and y coordinates are generated in negative values and map is not created.

    I will be thankful for support.

  • #2
    Typically you can get the area centriods when the shape file is convert to a Stata data file.

    Using the shape file located at https://earthworks.stanford.edu/cata...nlevel2admin10

    Code:
    spshape2dta GISPORTAL_GISOWNER01_PAKISTANLEVEL2ADMIN10
    use GISPORTAL_GISOWNER01_PAKISTANLEVEL2ADMIN10
    spmap using GISPORTAL_GISOWNER01_PAKISTANLEVEL2ADMIN10_shp, id(_ID) /// 
     label(label(D_NAME) x(_CX) y(_CY) size(*.5))
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	165.8 KB
ID:	1544157

    Comment


    • #3
      Thanks. My problem solved. I was using shp2dta instead of spshape2dta.

      These districts are part of higher levels called Provinces. can I highlight (may be bold or different color) Province boundaries on this map.

      Regards

      Comment

      Working...
      X