Update map generator for federal election.
This commit is contained in:
142
map-generator/shptojson/shptojson.c
Normal file
142
map-generator/shptojson/shptojson.c
Normal file
@@ -0,0 +1,142 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Project: Shapelib
|
||||
* Purpose: Sample application for dumping contents of a shapefile to
|
||||
* the terminal in human readable form.
|
||||
* Author: Frank Warmerdam, warmerdam@pobox.com
|
||||
*
|
||||
******************************************************************************
|
||||
* Copyright (c) 1999, Frank Warmerdam
|
||||
*
|
||||
* SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
|
||||
******************************************************************************
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "shapefil.h"
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int nPrecision = 15;
|
||||
int first;
|
||||
const char *divison_name;
|
||||
|
||||
/* -------------------------------------------------------------------- */
|
||||
/* Display a usage message. */
|
||||
/* -------------------------------------------------------------------- */
|
||||
if (argc != 2)
|
||||
{
|
||||
printf("Usage: shptojson shp_file\n");
|
||||
exit(1);
|
||||
}
|
||||
/* -------------------------------------------------------------------- */
|
||||
/* Open the passed shapefile. */
|
||||
/* -------------------------------------------------------------------- */
|
||||
SHPHandle hSHP = SHPOpen(argv[1], "rb");
|
||||
DBFHandle hDBF = DBFOpen(argv[1], "rb");
|
||||
if (hSHP == NULL || hDBF == NULL)
|
||||
{
|
||||
printf("Unable to open:%s\n", argv[1]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------- */
|
||||
/* Print out the file bounds. */
|
||||
/* -------------------------------------------------------------------- */
|
||||
int nEntities;
|
||||
int nShapeType;
|
||||
double adfMinBound[4];
|
||||
double adfMaxBound[4];
|
||||
SHPGetInfo(hSHP, &nEntities, &nShapeType, adfMinBound, adfMaxBound);
|
||||
|
||||
fprintf(stderr, "Shapefile Type: %s # of Shapes: %d\n\n",
|
||||
SHPTypeName(nShapeType), nEntities);
|
||||
|
||||
/* -------------------------------------------------------------------- */
|
||||
/* Skim over the list of shapes, printing all the vertices. */
|
||||
/* -------------------------------------------------------------------- */
|
||||
printf("[\n");
|
||||
for (int i = 0; i < nEntities; i++)
|
||||
{
|
||||
SHPObject *psShape = SHPReadObject(hSHP, i);
|
||||
|
||||
if (psShape == NULL)
|
||||
{
|
||||
fprintf(stderr,
|
||||
"Unable to read shape %d, terminating object reading.\n",
|
||||
i);
|
||||
break;
|
||||
}
|
||||
|
||||
divison_name = DBFReadStringAttribute(hDBF, i, 1);
|
||||
fprintf(stderr, "shape %s has %i parts\n", divison_name, psShape->nParts);
|
||||
|
||||
printf(" {\n");
|
||||
printf(" \"electorateId\": \"%08i\",\n", i);
|
||||
printf(" \"electorateName\": \"%s\",\n", divison_name);
|
||||
printf(" \"electorateType\": \"Division\",\n");
|
||||
printf(" \"parentElectorateName\": \"%s\",\n", divison_name);
|
||||
printf(" \"boundaryJson\": \"{\\\"type\\\": ");
|
||||
|
||||
if (psShape->nParts != 1)
|
||||
{
|
||||
printf("\\\"MultiPolygon\\\",\\\"coordinates\\\":[[[");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("\\\"Polygon\\\",\\\"coordinates\\\":[[");
|
||||
}
|
||||
|
||||
first = 1;
|
||||
for (int j = 0, iPart = 1; j < psShape->nVertices; j++)
|
||||
{
|
||||
if (iPart < psShape->nParts && psShape->panPartStart[iPart] == j)
|
||||
{
|
||||
iPart++;
|
||||
printf("]],[[");
|
||||
}
|
||||
else if (!first)
|
||||
{
|
||||
printf(",");
|
||||
}
|
||||
first = 0;
|
||||
printf("[%.*g,%.*g]", nPrecision, psShape->padfX[j],
|
||||
nPrecision, psShape->padfY[j]);
|
||||
}
|
||||
|
||||
if (i == (nEntities-1))
|
||||
{
|
||||
if (psShape->nParts != 1)
|
||||
{
|
||||
printf("]]]}\"\n }\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("]]}\"\n }\n");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (psShape->nParts != 1)
|
||||
{
|
||||
printf("]]]}\"\n },\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("]]}\"\n },\n");
|
||||
}
|
||||
}
|
||||
|
||||
SHPDestroyObject(psShape);
|
||||
}
|
||||
printf("]\n");
|
||||
|
||||
SHPClose(hSHP);
|
||||
DBFClose(hDBF);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
Reference in New Issue
Block a user