From 31a31c1fb9d46e9c6db1c836e03314b0b534194c Mon Sep 17 00:00:00 2001 From: Bibiko Date: Sat, 9 Oct 2010 23:16:37 +0000 Subject: =?UTF-8?q?=E2=80=A2=20enabled=20SRID=20editing=20of=20spatial=20f?= =?UTF-8?q?ields=20via=20WKT=20string=20-=20the=20SRID=20value=20will=20be?= =?UTF-8?q?=20appended=20at=20the=20WKT=20string=20like=20POINT(1=201),101?= =?UTF-8?q?=20which=20will=20be=20transformed=20to=20GeomFromText('POINT(1?= =?UTF-8?q?=201)',101)=20on=20the=20fly=20-=20otherwise=20the=20SRID=20inf?= =?UTF-8?q?ormation=20will=20be=20deleted=20after=20editing=20a=20geometry?= =?UTF-8?q?=20field?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MCPKit/MCPFoundationKit/MCPGeometryData.m | 33 ++++++++++++++-------- Source/SPCustomQuery.m | 2 +- Source/SPDataImport.m | 2 +- Source/SPStringAdditions.h | 2 ++ Source/SPStringAdditions.m | 27 ++++++++++++++++++ Source/SPTableContent.m | 5 ++-- 6 files changed, 55 insertions(+), 16 deletions(-) diff --git a/Frameworks/MCPKit/MCPFoundationKit/MCPGeometryData.m b/Frameworks/MCPKit/MCPFoundationKit/MCPGeometryData.m index cdb13348..f8c1bbf6 100644 --- a/Frameworks/MCPKit/MCPFoundationKit/MCPGeometryData.m +++ b/Frameworks/MCPKit/MCPFoundationKit/MCPGeometryData.m @@ -24,12 +24,11 @@ #import "MCPGeometryData.h" - #define SIZEOF_STORED_UINT32 4 #define SIZEOF_STORED_DOUBLE 8 #define POINT_DATA_SIZE (SIZEOF_STORED_DOUBLE*2) #define WKB_HEADER_SIZE (1+SIZEOF_STORED_UINT32) -#define BUFFER_START 4 +#define BUFFER_START 0 @implementation MCPGeometryData @@ -103,9 +102,8 @@ */ - (NSString*)wktString { - char byteOrder; - UInt32 geoType, numberOfItems, numberOfSubItems, numberOfSubSubItems, numberOfCollectionItems; + UInt32 geoType, srid, numberOfItems, numberOfSubItems, numberOfSubSubItems, numberOfCollectionItems; st_point_2d aPoint; NSUInteger i, j, k, n; // Loop counter for numberOf...Items @@ -116,6 +114,9 @@ if (bufferLength < WKB_HEADER_SIZE) return @"Header Error"; + memcpy(&srid, &geoBuffer[0], SIZEOF_STORED_UINT32); + ptr += SIZEOF_STORED_UINT32; + byteOrder = geoBuffer[ptr]; if(byteOrder != 0x1) @@ -129,7 +130,7 @@ case wkb_point: memcpy(&aPoint, &geoBuffer[ptr], POINT_DATA_SIZE); - return [NSString stringWithFormat:@"POINT(%.16g %.16g)", aPoint.x, aPoint.y]; + return [NSString stringWithFormat:@"POINT(%.16g %.16g)%@", aPoint.x, aPoint.y, (srid) ? [NSString stringWithFormat:@",%u",srid]: @""]; break; case wkb_linestring: @@ -141,7 +142,7 @@ [wkt appendFormat:@"%.16g %.16g%@", aPoint.x, aPoint.y, (i < numberOfItems-1) ? @"," : @""]; ptr += POINT_DATA_SIZE; } - [wkt appendString:@")"]; + [wkt appendFormat:@")%@", (srid) ? [NSString stringWithFormat:@",%u",srid]: @""]; return wkt; break; @@ -160,7 +161,7 @@ } [wkt appendFormat:@")%@", (i < numberOfItems-1) ? @"," : @""]; } - [wkt appendString:@")"]; + [wkt appendFormat:@")%@", (srid) ? [NSString stringWithFormat:@",%u",srid]: @""]; return wkt; break; @@ -173,7 +174,7 @@ [wkt appendFormat:@"%.16g %.16g%@", aPoint.x, aPoint.y, (i < numberOfItems-1) ? @"," : @""]; ptr += POINT_DATA_SIZE+WKB_HEADER_SIZE; } - [wkt appendString:@")"]; + [wkt appendFormat:@")%@", (srid) ? [NSString stringWithFormat:@",%u",srid]: @""]; return wkt; break; @@ -193,7 +194,7 @@ ptr += WKB_HEADER_SIZE; [wkt appendFormat:@")%@", (i < numberOfItems-1) ? @"," : @""]; } - [wkt appendString:@")"]; + [wkt appendFormat:@")%@", (srid) ? [NSString stringWithFormat:@",%u",srid]: @""]; return wkt; break; @@ -219,7 +220,7 @@ ptr += WKB_HEADER_SIZE; [wkt appendFormat:@")%@", (i < numberOfItems-1) ? @"," : @""]; } - [wkt appendString:@")"]; + [wkt appendFormat:@")%@", (srid) ? [NSString stringWithFormat:@",%u",srid]: @""]; return wkt; break; @@ -341,7 +342,7 @@ } [wkt appendString:(n < numberOfCollectionItems-1) ? @"," : @""]; } - [wkt appendString:@")"]; + [wkt appendFormat:@")%@", (srid) ? [NSString stringWithFormat:@",%u",srid]: @""]; return wkt; break; @@ -361,7 +362,7 @@ { char byteOrder; - UInt32 geoType, numberOfItems, numberOfSubItems, numberOfSubSubItems, numberOfCollectionItems; + UInt32 geoType, srid, numberOfItems, numberOfSubItems, numberOfSubSubItems, numberOfCollectionItems; st_point_2d aPoint; NSUInteger i, j, k, n; // Loop counter for numberOf...Items @@ -383,6 +384,9 @@ if (bufferLength < WKB_HEADER_SIZE) return nil; + memcpy(&srid, &geoBuffer[0], SIZEOF_STORED_UINT32); + ptr += SIZEOF_STORED_UINT32; + byteOrder = geoBuffer[ptr]; if(byteOrder != 0x1) @@ -409,6 +413,7 @@ [NSNumber numberWithDouble:y_max], nil], @"bbox", coordinates, @"coordinates", + [NSNumber numberWithUnsignedInt:srid], @"srid", @"POINT", @"type", nil]; break; @@ -463,6 +468,7 @@ [NSNumber numberWithDouble:y_max], nil], @"bbox", coordinates, @"coordinates", + [NSNumber numberWithUnsignedInt:srid], @"srid", @"POLYGON", @"type", nil]; break; @@ -487,6 +493,7 @@ [NSNumber numberWithDouble:y_max], nil], @"bbox", coordinates, @"coordinates", + [NSNumber numberWithUnsignedInt:srid], @"srid", @"MULTIPOINT", @"type", nil]; break; @@ -518,6 +525,7 @@ [NSNumber numberWithDouble:y_max], nil], @"bbox", coordinates, @"coordinates", + [NSNumber numberWithUnsignedInt:srid], @"srid", @"MULTILINESTRING", @"type", nil]; break; @@ -553,6 +561,7 @@ [NSNumber numberWithDouble:y_max], nil], @"bbox", coordinates, @"coordinates", + [NSNumber numberWithUnsignedInt:srid], @"srid", @"MULTIPOLYGON", @"type", nil]; break; diff --git a/Source/SPCustomQuery.m b/Source/SPCustomQuery.m index 097b5cd6..3938a72f 100644 --- a/Source/SPCustomQuery.m +++ b/Source/SPCustomQuery.m @@ -1907,7 +1907,7 @@ } else if([anObject isEqualToString:[prefs stringForKey:SPNullValue]]) { newObject = @"NULL"; } else if ([[columnDefinition objectForKey:@"typegrouping"] isEqualToString:@"geometry"]) { - newObject = [NSString stringWithFormat:@"GeomFromText('%@')", anObject]; + newObject = [(NSString*)anObject getGeomFromTextFromString]; } else if ([[columnDefinition objectForKey:@"typegrouping"] isEqualToString:@"bit"]) { newObject = [NSString stringWithFormat:@"b'%@'", ((![[anObject description] length] || [[anObject description] isEqualToString:@"0"]) ? @"0" : [anObject description])]; } else if ([[columnDefinition objectForKey:@"typegrouping"] isEqualToString:@"date"] diff --git a/Source/SPDataImport.m b/Source/SPDataImport.m index d467e0a1..98cbb697 100644 --- a/Source/SPDataImport.m +++ b/Source/SPDataImport.m @@ -1358,7 +1358,7 @@ } else { // Apply GeomFromText() for each geometry field if([geometryFields count] && [geometryFieldsMapIndex containsIndex:i]) { - [valueString appendFormat:@"GeomFromText('%@')", [mySQLConnection prepareString:cellData]]; + [valueString appendString:[mySQLConnection prepareString:[(NSString*)cellData getGeomFromTextFromString]]]; } else { [valueString appendFormat:@"'%@'", [mySQLConnection prepareString:cellData]]; } diff --git a/Source/SPStringAdditions.h b/Source/SPStringAdditions.h index 1826f888..260f44d9 100644 --- a/Source/SPStringAdditions.h +++ b/Source/SPStringAdditions.h @@ -67,6 +67,8 @@ static inline id NSMutableAttributedStringAttributeAtIndex (NSMutableAttributedS - (NSArray *)lineRangesForRange:(NSRange)aRange; - (NSString *)createViewSyntaxPrettifier; +- (NSString*)getGeomFromTextFromString; + - (NSString *)stringByRemovingCharactersInSet:(NSCharacterSet*)charSet options:(NSUInteger)mask; - (NSString *)stringByRemovingCharactersInSet:(NSCharacterSet*)charSet; diff --git a/Source/SPStringAdditions.m b/Source/SPStringAdditions.m index 2eafb4e9..a8695dbb 100644 --- a/Source/SPStringAdditions.m +++ b/Source/SPStringAdditions.m @@ -374,6 +374,33 @@ return 0.0; } +/** + * Create the GeomFromText() string according to a possible SRID value + */ +- (NSString*)getGeomFromTextFromString +{ + + NSString *geomStr = [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; + + if(![self rangeOfString:@")"].length || [self length] < 5) return @""; + + // No SRID + if([geomStr hasSuffix:@")"]) + return [NSString stringWithFormat:@"GeomFromText('%@')", geomStr]; + // Has SRID + else { + NSUInteger idx = [geomStr length]-1; + while(idx>1) { + if([geomStr characterAtIndex:idx] == ')') + break; + idx--; + } + return [NSString stringWithFormat:@"GeomFromText('%@'%@)", + [geomStr substringToIndex:idx+1], [geomStr substringFromIndex:idx+1]]; + } + +} + /** * Returns the minimum of a, b and c. */ diff --git a/Source/SPTableContent.m b/Source/SPTableContent.m index ab0a15c3..027f86a3 100644 --- a/Source/SPTableContent.m +++ b/Source/SPTableContent.m @@ -2340,6 +2340,7 @@ contextInfo:nil]; } + /** * Tries to write a new row to the database. * Returns YES if row is written to database, otherwise NO; also returns YES if no row @@ -2392,7 +2393,7 @@ [rowValue setString:@"CURRENT_TIMESTAMP"]; } else if ( [[NSArrayObjectAtIndex(dataColumns, i) objectForKey:@"typegrouping"] isEqualToString:@"geometry"] ) { - [rowValue setString:[NSString stringWithFormat:@"GeomFromText('%@')", ([rowObject isKindOfClass:[MCPGeometryData class]]) ? [rowObject wktString] : rowObject]]; + [rowValue setString:([rowObject isKindOfClass:[MCPGeometryData class]]) ? [[rowObject wktString] getGeomFromTextFromString] : [(NSString*)rowObject getGeomFromTextFromString]]; // Convert the object to a string (here we can add special treatment for date-, number- and data-fields) } else if ( [rowObject isNSNull] || ([rowObject isMemberOfClass:[NSString class]] && [[rowObject description] isEqualToString:@""]) ) { @@ -3565,7 +3566,7 @@ } else if([anObject isEqualToString:[prefs stringForKey:SPNullValue]]) { newObject = @"NULL"; } else if ([[columnDefinition objectForKey:@"typegrouping"] isEqualToString:@"geometry"]) { - newObject = [NSString stringWithFormat:@"GeomFromText('%@')", anObject]; + newObject = [(NSString*)anObject getGeomFromTextFromString]; } else if ([[columnDefinition objectForKey:@"typegrouping"] isEqualToString:@"bit"]) { newObject = [NSString stringWithFormat:@"b'%@'", ((![[anObject description] length] || [[anObject description] isEqualToString:@"0"]) ? @"0" : [anObject description])]; } else if ([[columnDefinition objectForKey:@"typegrouping"] isEqualToString:@"date"] -- cgit v1.2.3