Announcement

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

  • map2dta output empty

    Dear all,

    I am trying to create a map of Africa with a dot for every observation in dataset_A, where dataset_A contains latitude and longitude for each obs. I suppose the tmap package is the easiest to do this. I am already failing at converting a Mapinfo interchange format (mif) file to Stata datasets. The mif file I am using is the Africa file from http://maplibrary.org/library/stacks/Africa/index.htm. The folder contains a .mif and a .mid file. I tried to convert it to dta using mif2dta:

    Code:
    mif2dta using Africa, type(polygon) genid(ID) attributes(Africa_1) coordinates(Africa_1_coord)
    The directory is set to the folder containing the two files. I tried polyline and point instead as type (I don't know which is the right one), but no luck. With polyline I get ​​​​
    "​​​__000000 not found
    r(111);"
    and with the other two it creates a file containing values for ID only, the other variables all are empty. What am I doing wrong? Is this even the easiest package to use?

    Thank you in advance.

    Best,
    Marco

  • #2
    The site you link to contains maps of country outlines in ESRI shape file format. Download that instead and use shp2dta (from SSC) to convert to a useable format. To create a map, the most commonly used command for that is spmap, also from SSC. If you want to create maps with recognizable proportions, also install geo2xy (from SSC. In the following example, I show how to convert the shape file and plot some African capitals.

    Code:
    * convert the shapefile in the directory "Africa_SHP"
    shp2dta using Africa_SHP/Africa, database(map_data) coordinates(map_coor) genid(_ID) replace
    
    * input coordinates for some capitals and project the coordinates using
    * a Google Map projection
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str9(countryname capitalname) double capitallatitude float capitallongitude str2 countrycode
    "Algeria"  "Algiers"                 36.75      3.05 "DZ"
    "Angola"   "Luanda"     -8.833333333333334 13.216667 "AO"
    "Botswana" "Gaborone"  -24.633333333333333      25.9 "BW"
    "Burundi"  "Bujumbura" -3.3666666666666667     29.35 "BI"
    "Cameroon" "Yaounde"    3.8666666666666667 11.516667 "CM"
    "Chad"     "N'Djamena"                12.1 15.033333 "TD"
    "Comoros"  "Moroni"                  -11.7  43.23333 "KM"
    "Djibouti" "Djibouti"   11.583333333333334     43.15 "DJ"
    "Egypt"    "Cairo"                   30.05     31.25 "EG"
    "Eritrea"  "Asmara"     15.333333333333334  38.93333 "ER"
    end
    geo2xy capitallatitude capitallongitude, gen(c_lat c_lon)
    save "capitals.dta", replace
    
    * project the coordinates of the borders using a Google Map projection
    use "map_coor.dta"
    geo2xy _Y _X, replace
    save "xy_coor.dta", replace
    
    * create the map with the capitals
    use "map_data.dta", clear
    spmap using "xy_coor.dta", id(_ID) ///
        point(data("capitals.dta") xcoord(c_lon) ycoord(c_lat) fcolor(blue))
    and here's the map created:
    Click image for larger version

Name:	africa.png
Views:	1
Size:	539.1 KB
ID:	1396162

    Comment


    • #3
      Robert Picard Thank you, awesome answer! Is there a way of mapping two kinds of dots onto the map, in different colours?

      Comment


      • #4
        Well this may surprise some on this list but I don't use spmap so I would have to tackle the hefty help file to figure it out. Perhaps others can jump in with an spmap solution but this is how I would do this using standard Stata graph commands.

        Code:
        * convert the shapefile in the directory "Africa_SHP"
        shp2dta using Africa_SHP/Africa, database(map_data) coordinates(map_coor) genid(_ID) replace
        
        * input coordinates for some capitals and project the coordinates using
        * a Google Map projection
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input str9(countryname capitalname) double capitallatitude float capitallongitude str2 countrycode
        "Algeria"  "Algiers"                 36.75      3.05 "DZ"
        "Angola"   "Luanda"     -8.833333333333334 13.216667 "AO"
        "Botswana" "Gaborone"  -24.633333333333333      25.9 "BW"
        "Burundi"  "Bujumbura" -3.3666666666666667     29.35 "BI"
        "Cameroon" "Yaounde"    3.8666666666666667 11.516667 "CM"
        "Chad"     "N'Djamena"                12.1 15.033333 "TD"
        "Comoros"  "Moroni"                  -11.7  43.23333 "KM"
        "Djibouti" "Djibouti"   11.583333333333334     43.15 "DJ"
        "Egypt"    "Cairo"                   30.05     31.25 "EG"
        "Eritrea"  "Asmara"     15.333333333333334  38.93333 "ER"
        end
        
        * generate two categories of points
        gen cat = mod(_n,2)
        
        * append the country borders to the points to plot
        rename capitallatitude _Y
        rename capitallongitude _X
        append using "map_coor.dta"
        
        * convert lat/lon to xy coordinates
        geo2xy _Y _X, gen(ylat xlon)
        
        * show the projection details and compute the graph's height
        return list
        local yheight = 6 * `r(aspect)'
        
        line ylat xlon if !mi(_ID), lwidth(vthin) lcolor(gray) cmissing(n) ///
        ||  ///
        scatter ylat xlon if cat == 1, msymbol(smx) mcolor(red) ///
        ||  ///
        scatter ylat xlon if cat == 0, msymbol(smcircle) mcolor(blue) ///
            xsize(6) ysize(`yheight') ///
            ylabel(minmax, nogrid) yscale(off) ///
            xlabel(minmax, nogrid) xscale(off) ///
            plotregion(margin(small)) graphregion(margin(small)) ///
            legend(off)
        
        graph export Africa2points.png, width(1200) replace
        and the generated map:
        Click image for larger version

Name:	Africa2points.png
Views:	1
Size:	447.1 KB
ID:	1396180

        Comment


        • #5
          Robert Picard thank you for your creative answer! I can't quite get it to work though. First of all, when I keep the /// I get a lot of "option / not allowed R198". When I write it all in one line, I get "variable smx not found. r111". I have tried to use x and X instead, but I get the same error. What am I missing?

          Comment


          • #6
            The 3 forward slashes are line continuation characters but they are not supported when you paste text in Stata's Command window. You need to copy the whole example into a do-file and run the do-file as a whole.

            Comment


            • #7
              Originally posted by Robert Picard View Post
              Perhaps others can jump in with an spmap solution
              [ATTACH=CONFIG]n1396180[/ATTACH]
              -spmap- has an example in their help file:

              Code:
              use "Italy-OutlineData.dta", clear
              spmap using "Italy-OutlineCoordinates.dta", id(id) ///
              point(data("Italy-Capitals.dta") xcoord(xcoord) ///
              ycoord(ycoord) by(size) fc(Rainbow) legenda(on))
              Please also see the slide with title "Dot maps: example 1" at http://www.stata.com/meeting/italy12...t12_pisati.pdf

              Comment

              Working...
              X