// generate a svg void Layer::save_svg(const char *filename) { FILE *file; file = fopen(filename, "w"); svg_init(file); svg_comment("slice in for height=%f", height); for(std::vector<line*>::iterator it = segments->begin(); it != segments->end(); it++) { line *l = *it; svg_line(l->p1.x, l->p1.y, l->p2.x, l->p2.y); } svg_footer(); fclose(file); }
int main(){ int entityCount; int shapeType; double padfMinBound[4]; double padfMaxBound[4]; int i; int use_gal = 1; int use_dist = 0; //For josh //char sf_name[] = "/home/joshua/FultonCoData/Fultoncombinednd_10.shp"; //for sumanth //char sf_name[] = "/home/sumanth/Documents/eDemocracy/Files/Fultoncombinednd.shp"; //for alice char sf_name[]= "/home/altheacynara/Documents/saltLakeData/tl_2008_49035_tabblock.shp"; // char sf_name[]="/home/altheacynara/Documents/fultonData/Fultoncombinednd.shp"; //Eventually, this won't be hardcoded SHPHandle handle = SHPOpen(sf_name, "rb"); int fn_len = strlen(sf_name); char svg_filename[fn_len]; char gal_filename[fn_len]; char dst_filename[fn_len]; FILE *svg; strcpy(svg_filename, sf_name); strcpy(gal_filename, sf_name); strcpy(dst_filename, sf_name); svg_filename[fn_len-2] = 'v'; svg_filename[fn_len-1] = 'g'; gal_filename[fn_len-3] = 'G'; gal_filename[fn_len-2] = 'A'; gal_filename[fn_len-1] = 'L'; dst_filename[fn_len-3] = 'd'; dst_filename[fn_len-2] = 's'; dst_filename[fn_len-1] = 't'; //I know, the above isn't really robust enough. //Should be improved upon when the file name is no longer hardcoded SHPGetInfo(handle, &entityCount, &shapeType, padfMinBound, padfMaxBound); SHPObject **shapeList = malloc(entityCount*sizeof(SHPObject *)); //neighborList neighbors[entityCount]; struct neighbor_list *NLIST; double xCentList[entityCount]; double yCentList[entityCount]; double areaList[entityCount]; //populate the shapeList for(i=0; i<entityCount; i++){ shapeList[i] = SHPReadObject(handle,i); } printf("Shapelist populated.\n"); //delete file if it exists remove(svg_filename); //set up the SVG file pointer svg = fopen(svg_filename, "a+"); printf("SVG file opened for writing.\n"); //write header svg_header(svg); printf("SVG header printed.\n"); //Call colorArrange: int ndists=5; int *colorArray = malloc(entityCount*sizeof(int)); colorArrange(colorArray,entityCount,ndists, dst_filename); //write individual polygons for(i=0; i<entityCount; i++){ svg_polygon(*shapeList[i], svg, use_dist, colorArray); } printf("Polygons all printed.\n"); if(use_gal){ FILE *gal; int block, num_neigh, nblocks, temp_neigh; int count=0; gal= fopen(gal_filename, "r"); if(gal==NULL){ printf("Error: Could not open GAL file.\n"); return -1; } fscanf(gal, "%d", &nblocks); if(nblocks==entityCount){ printf("GAL block count matches shapefile block count. Proceeding...\n"); }else{ printf("GAL block count does not match. Exiting...\n"); exit(EXIT_FAILURE); } NLIST = malloc(nblocks * sizeof(struct neighbor_list)); while(fscanf(gal, "%d %d", &block, &num_neigh) != EOF) { NLIST[block].num_neighbors = num_neigh; if(num_neigh != 0) { count=0; NLIST[block].neighbors = malloc(num_neigh * sizeof(int)); while(count < num_neigh) { fscanf(gal, "%d", &temp_neigh); NLIST[block].neighbors[count] = temp_neigh; count++; } } } //Debugging: print the neighbor list of all blocks /*int i,j; for(i=0;i<nblocks;i++) { printf("%d %d\n", i, NLIST[i].num_neighbors); if(NLIST[i].num_neighbors > 0) for(j=0;j<NLIST[i].num_neighbors;j++) printf("%d ", NLIST[i].neighbors[j]); printf("\n"); }*/ //find centroids for every block for(i=0; i<entityCount; i++){ int lastPoint; int status; SHPObject block = *shapeList[i]; //Note that we're going to disregard holes, etc. if(block.nParts>1){ lastPoint = block.panPartStart[1]-1; }else{ lastPoint = block.nVertices-1; } status = polyCentroid(block.padfX, block.padfY, lastPoint, xCentList+i, yCentList+i, areaList+i); } printf("Centroids calculated.\n"); //write paths from centroid to centroid fputs("\t</g>\n", svg); fputs("\t<g\n\t\tid=\"layer2\">\n", svg); for(i=0; i<entityCount; i++){ svg_neighbors(*shapeList[i], NLIST[i], xCentList, yCentList, svg); } //svg_neighbors(*shapeList[20], NLIST[20], xCentList, yCentList, svg); printf("Contiguity paths drawn.\n"); //this is the section that's screwing up //Free NLIST for(i=0; i<entityCount; i++) { printf("i is %d\n", i); printf("NLIST[i]\n"); free(NLIST[i].neighbors); NLIST[i].neighbors = NULL; } free(NLIST); NLIST = NULL; fclose(gal); //end section that's screwing up } //write footer svg_footer(svg); printf("SVG footer printed.\n"); for(i=0; i<entityCount; i++){ SHPDestroyObject(shapeList[i]); } SHPClose(handle); fclose(svg); free(colorArray); return 0; }
int main(int argc, char *argv[]) { struct rooted_tree *tree; struct parameters params; int align_leaves; int with_scale_bar; enum display_status status; void (*node_destroyer)(struct rnode *) = NULL; params = get_params(argc, argv); if (params.svg) { set_svg_parameters(params); if(! svg_init()) { fprintf (stderr, "%s\n", get_last_error_message()); exit(EXIT_FAILURE); } node_destroyer = destroy_svg_node_data; } while (NULL != (tree = parse_tree())) { align_leaves = is_cladogram(tree); /* show scale bar IFF tree is NOT a cladogram. Since * is_cladogram() takes some time to run, we just look * up 'align_leaves' which has the same value. */ with_scale_bar = !align_leaves; /* User can also suppress scale bar */ if (params.no_scale_bar) with_scale_bar = FALSE; if (params.svg) { svg_header(leaf_count(tree), with_scale_bar); svg_run_params_comment(argc, argv); status = display_svg_tree(tree, align_leaves, with_scale_bar, params.branch_length_unit); switch(status) { case DISPLAY_OK: break; assert(0); case DISPLAY_MEM_ERROR: perror(NULL); exit(EXIT_FAILURE); /* The following two should never happen */ case DISPLAY_UNKNOWN_STYLE: default: assert(0); } svg_footer(); } else { prettify_labels(tree); status = display_tree(tree, params.width, align_leaves, params.inner_label_pos, with_scale_bar, params.branch_length_unit, params.scale_zero_at_root); switch(status) { case DISPLAY_OK: break; assert(0); case DISPLAY_MEM_ERROR: perror(NULL); exit(EXIT_FAILURE); /* The following two should never happen */ case DISPLAY_UNKNOWN_STYLE: default: assert(0); } } destroy_tree_cb(tree, node_destroyer); } return 0; }