3

I'm pretty sure this is more a of a math question, but I'll phrase it in the context of UIView and iPad-related objective C

I am importing raw data from a mapping file I have created from some public domain material downloaded elsewhere, then split out to isolate various regions within the map. Each region has a number of sub-regions, much like, for example, the continental US and then the various states which appear within the US, and then each sub-region is broken down again, into, let's say, counties.

Each state, and each county has a bounding box which tells me the origin, the width, and height each is.

In my initial setup, I created a separate view for each state, and then another view for each county. The polygon representing the area of the state/county was rendered (obviously with the county on top of the state so it would be visible) relative to a view I created through interface builder, called mainContainerView. This initial setup worked correctly.

Now I am trying to change things a bit, by adding the counties to the UIView holding the polygon for the state, so I will be able to overlay the state as a clipping mask on the counties. The problem is that no matter what I try, I cannot seem to get the county to translate to the right place within the state's view.

It seems like it should be straightforward addition or subtraction as the scaling for each item is exactly the same, and I'm not trying to do any major transformations, so I do not believe the CFAffineTransformation family is needed.

I can post code if necessary, but I'm not trying to get someone to write my program for me; I just want someone to point me in the right direction here, by giving me a suggestion on how to set the county relative to the state within the state's view.

As per a request, here's the relevant code that I am working on right now. This code does not work, but it gives you the idea as to what I'm doing. Posting sample data is a little more difficult, as it involves arrays of points and data extracted from a .SHP file designed to produce a map (and subdivisions). I'll include some comments in the code with some real point values as I step through the program to show you what's happening to them.

MASK_MAX_EASTING, MASK_MAX_NORTHING, MASK_MIN_EASTING, and MASK_MIN_NORTHING are constants which define the bounding box for the entire map of the country when made up of states.

DIST_MAX_EASTING, DIST_MAX_NORTHING, DIST_MIN_EASTING, and DIST_MIN_NORTHING are constants which define the bounding box for a map of the country when made up of the counties. The scales of the two maps are slightly different, so, by using the different bounding boxes, I've been able to scale the two maps to the same size.

-(void)didLoadMap:(NSNotification *)notification {

id region = [notification object];
ShapePolyline *polygon = [region polygon];

if ([notification name] == @"MapsLoadingForState") {

    // m_nBoundingBox is an array which contains the RAW northing and easting values for each subdivision.  [0] - west limit, [1] - south limit, [2] - east limit, [3] - north limit.

    // The code below, combined with the drawrect method in DrawMap.m (below) puts all the states on the map in precisely the right places, so for the state maps, it works just fine.

    CGFloat originX = ((polygon->m_nBoundingBox[0]-MASK_MIN_EASTING)*stateScaleMultiplier)+([mainContainerView frame].size.width/2);

    CGFloat originY = ((MASK_MAX_NORTHING-(polygon->m_nBoundingBox[3]))*stateScaleMultiplier)+[mainContainerView frame].origin.y;  
    CGFloat width = polygon->m_nBoundingBox[2] - polygon->m_nBoundingBox[0];
    CGFloat height = polygon->m_nBoundingBox[3] - polygon->m_nBoundingBox[1];

    CGFloat scaledWidth = width*stateScaleMultiplier;
    CGFloat scaledHeight = height*stateScaleMultiplier;
    UIColor *subViewColor = [UIColor colorWithRed:0.0 green:1.0 blue:1.0 alpha:0.0];

    stateMapView = [[DrawMap alloc] initWithFrame:CGRectMake(originX, originY, scaledWidth, scaledHeight)];
    [stateMapView setBackgroundColor:subViewColor];

    [stateMapView setStateScale:stateScaleMultiplier];
    [stateMapView setCountyScale:countyScaleMultiplier];  // Not actually needed.

    [stateMapView setClippingMask:polygon];
    UIColor *colorMask = [UIColor colorWithWhite:1.0 alpha:1.0];
    [stateMapView setForeground:colorMask];

    [states addObject:stateMapView];                      // Add the state map view to an array (for future use)
    [mapView addSubview:stateMapView];  // MapView is a UIView of equivalent size and shape as mainContainerView.

} else {

    // This is where the problems occur.  

    CGFloat originX = (polygon->m_nBoundingBox[0]-DIST_MIN_EASTING);  // 4431590 (raw data)
    originX *= countyScaleMultiplier;  // 303.929108
    originX += ([mainContainerView frame].size.width/2);  // 815.929077

    CGFloat originY = (DIST_MAX_NORTHING-polygon->m_nBoundingBox[3]); 4328997 
    originY *= countyScaleMultiplier;  // 296.893036
    originY -= [mainContainerView frame].origin.y;  // 340.893036

    CGRect frame = [stateMapView frame];  // Dummy variable created for watches in the debugger.  x=856.237183, y=332.169922 width=34.3800087, height=28.7534008

    // When I was invoking DrawMap.h and the included drawrect method, the county map would easily be displayed in the right place, as you can see by the values above.

    // This is where I think the problem is.  The X value is WAY off as far as I can tell.
    originX -= frame.origin.x; // -40.3081055 
    originY -= frame.origin.y; // 8.72311401

    CGPoint countyOrigin = CGPointMake(originX,originY);

    // Translate the county's origin so it is relative to the origin of stateMapView, not MainContainerView (doesn't work)

    [stateMapView addCountyMap:[region polygon] withColor:winner translatedBy:countyOrigin];
    [stateMapView setNeedsDisplay];
}

I am aware that there are several issues with this code and some stuff outside the scope of this question may make a few of you raise an eyebrow (or two) but this is definitely a work in progress...

Here's the relevant code from DrawMap.m; I've cut a bunch of stuff out because it is extraneous.

- (void)drawRect:(CGRect)rect {

// Set up

for (int i=0;i<[countyMaps count];i++) {

    // Draw the polygon.

    [[countyColors objectAtIndex:i] setFill];

    [self drawPolygon:[countyMaps objectAtIndex:i]
           usingScale:stateScale
         translatedBy:CGPointMake([[countyTranslations objectAtIndex:2*i] floatValue],
                                  [[countyTranslations objectAtIndex:2*i+1] floatValue])]; 

}

// Set the blend mode to multiply

CGContextSetBlendMode(context, kCGBlendModeMultiply);

// Draw a path with clippingMask

[[UIColor colorWithWhite:1.0 alpha:1.0] setFill];
// CGPoint translate = CGPointMake(0,0);

[self drawPolygon:clippingMask usingScale:stateScale translatedBy:CGPointMake(0,0)];

}

-(void)drawPolygon:(ShapePolyline *)aPolygon usingScale:(float)mapScale translatedBy:(CGPoint)trans {

for (int j=0;j<[aPolygon numParts];j++) {

    UIBezierPath *path = [UIBezierPath bezierPath];

    [path setLineJoinStyle:kCGLineJoinRound];

    int startIndex = [[[aPolygon m_Parts] objectAtIndex:j] intValue];
    int endIndex = [aPolygon numPoints];

    CGPoint startPoint;
    [[[aPolygon m_Points] objectAtIndex:startIndex] getValue:&startPoint];

    startPoint.x *=mapScale;
    startPoint.y *=mapScale;

    startPoint.x -= trans.x;
    startPoint.y -= trans.y;

    [path moveToPoint:startPoint];


    if (j+1 != [aPolygon numParts]){ 

        endIndex = [[[aPolygon m_Parts] objectAtIndex:j+1] intValue];
    }

    for (int k=startIndex+1; k<endIndex; k++)
    {
        CGPoint nextPoint;
        [[[aPolygon m_Points] objectAtIndex:k] getValue:&nextPoint];

        nextPoint.x *= mapScale;
        nextPoint.y *= mapScale;

        nextPoint.x -= trans.x;
        nextPoint.y -= trans.y;

        [path addLineToPoint:nextPoint];

    }
    [path closePath];
    // [path stroke];
    [path fill];
}

}

This tome is really may be too much information, or it may not be enough. Either way, hopefully by adding code, I've given you some information to go on...

4

1 回答 1

1

-SOLVED-

And it was so simple. I'm surprised it took me this long to figure it out, as I was right in my initial question - it was simple addition and subtraction:

All translations are now done inside the methods which render the polygons. For each point in the polygon, I needed to add the origin of the state's view, and subtract the origin of the county's bounding box, then subtract 44 from the Y-value (the height of the control bar).

This, I think, is an example of over-thinking a problem, getting frustrated, over-thinking more, only to find out three days later that the answer is staring you in the face, waving a red flag, and shouting, "I'M OVER HERE!!!!"

于 2011-06-13T11:57:00.437 回答