What does a provincial map of China tell us that a prefecture map does not, and vice versa? Using 2024 NASA Black Marble data, the first map shows where recorded nighttime light is concentrated across province-level units. The second reveals differences within those provinces. Drawing the maps in Stata is straightforward once the data are aligned. The key step is deciding which observation belongs to which polygon, and leaving uncertain matches blank.
Three files, three kinds of identifiers
A map has two ingredients that often come from different sources: the quantity to display and the geometry used to display it. The World Bank table tells us how much light was recorded for a unit in 2024. The GADM shapefile supplies the outlines for the map. A join tells Stata which value to colour which outline. Keeping those roles separate helps diagnose errors that are hard to see in the finished figure.
| File or dataset | Identifier used by the code | Purpose |
|---|---|---|
| World Bank Space2Stats radiance table | adm2cd_c and year | Identify the annual observation; the value to map is ntl_sum. |
| ADM2 crosswalk | adm2cd_c and gid_2 | Connect the World Bank observation to the corresponding GADM unit. |
| GADM attribute and coordinate data | gid_2 in attributes; Stata _ID in both | Join radiance to attributes by GADM identifier, then draw the matching geometry by Stata’s spatial ID. |
At ADM1, the do-file constructs a normalized province name instead of reading a separate crosswalk. At ADM2, it uses the checked ID crosswalk in the replication folder. The maps are otherwise built on the same principle: keep the geography as the base and attach the data to it.
1. Start with annual radiance, not a satellite image
The inputs are annual nighttime-light summaries from the World Bank’s Space2Stats Stata package. At ADM2, the World Bank dataset aggregates NASA Black Marble observations to its official administrative boundaries. The Stata workflow starts with those totals. It does not read satellite pixels or recalculate radiance on the GADM shapefile.
use "data/China_BlackMarble_ADM2_annual.dta", clear
isid adm2cd_c year
keep if year == 2024
keep adm2cd_c ntl_sum
isid verifies that each World Bank ADM2 identifier appears at most once per year before the script selects 2024. The retained ntl_sum measures total recorded radiance, so it reflects both the intensity and the extent of recorded light. It is not radiance per square kilometre, and it should not be read as local GDP.
Fixing the year in the do-files is deliberate. A new Space2Stats release should not silently change a published map when someone reruns the replication. Likewise, the annual observation is already the source’s annual radiance summary. Substituting a sum of the monthly files would change the input series. For this demonstration, geography changes between the two figures; the year and satellite product do not.
2. Convert the boundary file into something Stata can draw
foreach ext in shp dbf shx prj cpg {
copy "data/gadm41_CHN_2.`ext'" ///
"output/GADM41_ADM2_input.`ext'", replace
}
quietly cd "output"
capture noisily spshape2dta GADM41_ADM2_input, ///
saving(China_GADM41_ADM2) replace
local rc = _rc
quietly cd ..
if `rc' exit `rc'
spshape2dta writes two files: an attribute dataset (one record per map unit) and a coordinate dataset ending in _shp.dta. Stata’s _ID links each attribute record to the coordinates used to draw it. This is a different key from the World Bank data identifier and from GADM’s own GID_2. The script converts a copy in output/, leaving the input shapefile intact.
The extensions matter. The .shp holds the geometry, .dbf holds the associated attributes and .shx indexes the shapes; the .prj and .cpg describe the coordinate reference and character encoding. Keep the components together with the same basename. The script runs spshape2dta from the staging folder, captures the return code, and returns to the project root even if the conversion fails. That makes a missing or malformed shapefile easier to diagnose.
3. ADM1: reconcile province names before merging
The province-level script creates a common name key on both sides. It lowercases the names, strips accents and punctuation, and deals explicitly with variants such as Inner Mongolia and Neimenggu. These lines show the normalization and one alias:
gen str80 province_key = ustrlower(ustrnormalize(province_name,"nfd"))
replace province_key = ustrregexra(province_key,"[^a-z]","")
replace province_key = "neimenggu" if ///
regexm(province_key,"^(innermongolia|neimenggu|neimongol)")
The code checks that the normalized key is unique on the radiance side, then merges onto the map attributes. Taiwan remains in the source panel but has no corresponding polygon in this GADM file. The script handles that case explicitly and stops if any other World Bank province with a nonmissing value is left unmatched. A province should not silently disappear because its name is spelled differently.

The ADM1 graph is the useful starting point for a nationwide overview. It fills an entire province with one colour even if most of its recorded radiance is concentrated in a few cities. That is a consequence of the aggregation level, not a claim that the province is uniformly lit.
4. ADM2: merge on a checked crosswalk and keep unmatched polygons
At ADM2, a name alone is a poor join key. The crosswalk was built by matching normalized province and prefecture names together, with explicit aliases for a small number of spelling variants. Ambiguous matches were left out. It records the World Bank ID (adm2cd_c) and the GADM ID (gid_2), so the do-file can merge on stable identifiers without guessing from names at run time.
import delimited using "data/ADM2_WorldBank_GADM_Crosswalk.csv", ///
varnames(1) clear stringcols(_all)
isid gid_2
isid adm2cd_c
merge 1:1 adm2cd_c using "`lights'", keep(master match)
assert _merge==3
drop _merge
save "`walks'"
Here lights and walks are Stata temporary files. The first merge attaches the 2024 radiance total to each crosswalk entry. assert _merge==3 makes a missing World Bank match an error. The second merge starts from the GADM shapes and keeps geometry records without a match:
isid gid_2
keep if substr(gid_2,1,4)=="CHN."
merge 1:1 gid_2 using "`walks'", assert(master match)
The result is 330 matched units out of 344 mainland GADM units. The other 14 remain visible in gray. They have no matched observation; their radiance is not zero. An identifier match locates the corresponding unit on the map, but the value still comes from aggregation on World Bank boundaries. The two boundary files need not have identical outlines.

Why are the two merge rules different? The crosswalk is a list of claimed matches: every one of its entries should have a World Bank row for the mapped year. Once radiance is attached, the map has a different requirement: all eligible polygons should be drawn, including those with no crosswalk entry. For the second merge, an unmatched GADM record is acceptable; a crosswalk record that points to no eligible polygon is an error. Reversing the second merge or keeping matches only would hide precisely the areas where coverage is incomplete.
quietly count if _merge==3 & !missing(ntl_sum)
local observed = r(N)
quietly count
local regions = r(N)
display "MAPPED=" `observed' " OF " `regions' " YEAR " `latest'
The numerator counts units with both a match and an observed radiance value. The denominator counts the eligible GADM polygons. Publishing both numbers makes the gray areas interpretable. It also gives a replicator a concrete check: the supplied inputs give 330 mapped units out of 344 in 2024.
5. Draw the maps and label the scale
The ADM2 script divides the totals by 1,000 for the legend; the ADM1 script divides by 1,000,000. These positive rescalings change the displayed units without changing the rank order. spmap uses five quantile classes separately at each level, with thin outlines and a separate missing-data key. The lines below show the ADM2 drawing options; the full do-file adds the title, legend layout and source note.
generate double light_k = ntl_sum/1000
format light_k %8.1f
spmap light_k using "output/China_GADM41_ADM2_shp.dta", id(_ID) ///
clmethod(quantile) clnumber(5) ///
fcolor("222 239 236" "179 216 209" "123 180 173" ///
"63 132 131" "28 78 87") ///
ocolor("107 121 124" ..) osize(vvthin ..) ///
ndfcolor(gs14) ndocolor("107 121 124") ndsize(vvthin) ///
ndlabel("Unmatched / no data")
A dark prefecture ranks high among prefectures, and a dark province ranks high among provinces. The two colours do not identify a common radiance threshold.
The palette is sequential: low totals are light teal and higher totals are darker. Thin boundaries preserve the shape of small prefectures without competing with the fills. The gray category uses a distinct label so readers can distinguish a missing match from the lowest observed class. The PDF preserves vector outlines, while the 3600-pixel PNG is convenient for slides and web pages.
Which map answers which question?
Use ADM1 to show the broad distribution of light across provinces. Use ADM2 to see how much spatial detail the provincial view hides. A province shaded in a high quantile can contain prefectures from several quantiles. That is not a contradiction: the province’s total and the prefecture’s total answer questions at different scales. Similarly, these maps compare totals, not light intensity per unit of area. A large prefecture may record more total light than a smaller one even if its average radiance is lower.
Run the replication
See the China Black Marble replication folder on GitHub for the do-files, data and README. The scripts expect the China ADM1 and ADM2 boundary components in data/; the original files are available from GADM 4.1. With Stata 17 or newer, install spmap once and set the working directory to the project root:
ssc install spmap
do "code/00_MASTER.do"
Each annual do-file also runs independently. The annual scripts use local inputs and create output/ at runtime, exporting a vector PDF and a 3600-pixel PNG per map. The replication folder does not contain pre-rendered maps.
6. Map a single month at ADM2
The World Bank Space2Stats release provides monthly NASA Black Marble summaries for 2012–2025. The map below is the December 2025 export from 03_China_BlackMarble_ADM2_Monthly_Map.do. Stata matched a reported value to 330 of the 344 mainland ADM2 polygons. The remaining polygons appear in gray.

Choose the month and locate the input
Set target at the top of the do-file. Stata extracts the year and month, checks that they fall within the release, and looks for the original World Bank CSV for that year in data/. If the file is absent, Stata reads the same CSV directly from the catalog. Each CSV contains global ADM2 data for all available months of that year, so changing only the month does not change which file is read. The boundary files are local in both cases.
local target "2025-12"
local filename "ntl_adm2_monthly_`yr'.csv"
local source "`data'/`filename'"
capture confirm file "`source'"
if _rc {
local rid = 95686 + (`yr' - 2012)
local wb "https://datacatalogfiles.worldbank.org"
local source "`wb'/ddh-published/0066940/DR00`rid'/`filename'"
}
capture noisily import delimited using "`source'", clear ///
varnames(1) case(lower) bindquote(strict) asdouble
Keep one observation per prefecture
The source date is an ISO string. The first seven characters select the month, irrespective of the recorded day. After filtering to China and the requested month, isid checks that each World Bank ADM2 code occurs only once. The script keeps the published ntl_sum and leaves missing radiance unchanged.
capture confirm variable iso_a3
if !_rc keep if upper(strtrim(iso_a3)) == "CHN"
replace adm2cd_c = upper(strtrim(adm2cd_c))
keep if substr(adm2cd_c, 1, 3) == "CHN" ///
& substr(strtrim(date), 1, 7) == "`target'"
isid adm2cd_c
Join the data to the map in two steps
The first merge uses the World Bank code, adm2cd_c, to attach radiance to the crosswalk. The second merge uses gid_2 to attach that result to the GADM attributes generated by spshape2dta. It preserves polygons without a match. Stata’s _ID then links each attribute row to its coordinates for drawing. The published radiance totals are attached to a display geometry; no satellite pixels are reaggregated.
import delimited using "`crosswalk'", clear varnames(1) ///
case(lower) stringcols(_all)
isid adm2cd_c
isid gid_2
merge 1:1 adm2cd_c using "`lights'", keep(master match) nogen
save "`walks'"
use "`out'/China_GADM41_ADM2_monthly.dta", clear
capture confirm variable GID_2
if !_rc rename GID_2 gid_2
isid gid_2
keep if substr(gid_2, 1, 4) == "CHN."
merge 1:1 gid_2 using "`walks'", assert(master match) nogen
Read the legend before comparing months
light_k expresses total radiance in thousands. The map classifies the reported values into five within-month quantile groups. Color therefore shows a prefecture’s position in that month’s distribution. To compare absolute levels across two months, calculate a single set of breakpoints and use clmethod(custom) with the same clbreaks() and palette in both maps.
generate double light_k = ntl_sum / 1000
format light_k %8.1f
graph set window fontface "Palatino Linotype"
spmap light_k using "`out'/China_GADM41_ADM2_monthly_shp.dta", ///
id(_ID) clmethod(quantile) clnumber(5) ///
fcolor("222 239 236" "179 216 209" "123 180 173" ///
"63 132 131" "28 78 87") ///
ocolor("107 121 124" ..) osize(vvthin ..) ///
ndfcolor(gs14) ndocolor("107 121 124") ndsize(vvthin) ///
ndlabel("Unmatched / no data") legstyle(2) legorder(lohi) ///
legend(position(6) ring(1) cols(3) rowgap(1.3) ///
colgap(3) symxsize(6) size(small) ///
region(lstyle(none)) ///
title("Total radiance (thousands)", size(small))) ///
title("{bf:China's nighttime lights, `nice_month'}", ///
size(medlarge)) ///
subtitle("NASA Black Marble monthly totals by ADM2 unit", ///
size(small) color(gs5)) ///
note("Sources: World Bank Space2Stats; GADM 4.1." ///
"`observed' of `regions' ADM2 units with data; five quantiles.", ///
size(vsmall) color(gs6)) ///
graphregion(color(white) margin(medsmall))
Run the monthly do-file independently from the project root or code/. It creates output/ at runtime and exports a vector PDF and a 3600-pixel PNG. The export shown above was produced in Stata.
local stem "China_BlackMarble_ADM2_Monthly_`target'"
graph export "`out'/`stem'.pdf", replace
graph export "`out'/`stem'.png", width(3600) replace
Three checks before interpreting an unexpected map
- The shapefile does not convert. Check the matching basenames and all five components in
data/, and run from the project root orcode/. - The join stops. Check the first failing
isidorassert. Duplicate keys, a different year or a different boundary release can change the matching problem. Do not remove the check to make the script finish. - The darkest areas or grey areas look different across levels. Read the legend before comparing colours. The maps use separate quantiles, and gray marks missing matches, not low light.
Related posts
Measuring nighttime lights across 228 countries and territories, 1992–2025 uses a separate harmonized DMSP–VIIRS series for comparisons over time. Portable Stata Projects: Running the Same Do-File on Any Computer explains the project-root convention used here.