I tried using C++ with Mapnik to read a shapefile located in path/to/shapefile
. The shapefile includes .shp, .shx, .prj, .dbf, etc. files. The shapefile can be read and rendered successfully, including its features. However, the context within each feature contains nothing (it should contain a map with descriptors and attributes).
I have successfully read the DBF file in the shapefile folder using other tools (like QGIS), and the attributes were displayed. I also tried another shapefile, but the result was the same.
What could be causing this problem? What steps should I take to fix it?
Environment: Win10; Mapnik 2023-06-12#3 built by vcpkg; VS 2022.
The code is as following:
int main(int argc, char** argv)
{
mapnik::setup();
try
{
mapnik::datasource_cache::instance().register_datasources("path/to/input");
const std::string srs_lcc =
"+proj=lcc +ellps=GRS80 +lat_0=49 +lon_0=-95 +lat+1=49 +lat_2=77 +datum=NAD83 +units=m +no_defs";
const std::string srs_merc = "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 "
"+units=m +nadgrids=@null +wktext +no_defs +over";
mapnik::Map map(3840, 2160);
map.set_background(mapnik::color(255, 255, 255));
map.set_srs(srs_merc);
mapnik::parameters params;
params["type"] = "shape";
params["file"] = "path/to/shapefile";
mapnik::layer lyr("Provinces", srs_lcc);
lyr.set_datasource(mapnik::datasource_cache::instance().create(params));
map.add_layer(lyr);
mapnik::query q(lyr.envelope());
auto features = lyr.datasource()->features(q);
while (auto feature = features->next())
{
auto context = feature->context(); // Problem here
}
}
catch (...)
{
std::cout << "### Unknown exception." << std::endl;
}
}