Skip to content

Commit

Permalink
Merge pull request #4187 from osmandapp/fix_import_gpx
Browse files Browse the repository at this point in the history
fix import gpx
  • Loading branch information
alex-osm authored Dec 11, 2024
2 parents 268d6ae + 4912c1c commit 307bf8d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,12 @@ - (void)updateAnalysis
}
else
{
OASGpxTrackAnalysis *analysis = _gpx && _gpx.dataItem ? [_gpx.dataItem getAnalysis] : nil;
_analysis = _doc
? [_doc getAnalysisFileTimestamp:0
fromDistance:nil
toDistance:nil
pointsAnalyzer:[OASPlatformUtil.shared getTrackPointsAnalyser]]
: analysis;
: _gpx && _gpx.dataItem ? [_gpx.dataItem getAnalysis] : nil;
}
}

Expand Down
68 changes: 20 additions & 48 deletions Sources/Helpers/OAGPXImportUIHelper.mm
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ @implementation OAGPXImportUIHelper

NSURL *_importUrl;
NSString *_importGpxPath;
NSString *_importGpxRelativePath;
OASGpxFile *_doc;
NSString *_newGpxName;
}
Expand All @@ -57,7 +56,6 @@ - (instancetype) initWithHostViewController:(UIViewController *)hostVC
_hostVC = hostVC;
_app = [OsmAndApp instance];
_importGpxPath = [_app.gpxPath stringByAppendingPathComponent:kImportFolderName];
_importGpxRelativePath = kImportFolderName;
}
return self;
}
Expand All @@ -69,19 +67,7 @@ - (void) onImportClicked

- (void) onImportClickedWithDestinationFolderPath:(NSString *)destPath
{
if (destPath)
{
NSString *trimmedDestPath = [self trim:destPath];
_importGpxPath = [_app.gpxPath stringByAppendingPathComponent:trimmedDestPath];
_importGpxRelativePath = trimmedDestPath;
}
else
{
_importGpxPath = [_app.gpxPath stringByAppendingPathComponent:kImportFolderName];
_importGpxRelativePath = kImportFolderName;
}


_importGpxPath = [_app.gpxPath stringByAppendingPathComponent:destPath ? [self trim:destPath] : kImportFolderName];
NSArray<UTType *> *contentTypes = @[[UTType importedTypeWithIdentifier:@"com.topografix.gpx" conformingToType:UTTypeXML],
[UTType importedTypeWithIdentifier:@"com.google.earth.kmz" conformingToType:UTTypeXML],
[UTType importedTypeWithIdentifier:@"com.google.earth.kml" conformingToType:UTTypeXML]];
Expand Down Expand Up @@ -171,7 +157,8 @@ - (void) showImportGpxAlert:(NSString *)title
NSFileManager *fileMan = [NSFileManager defaultManager];
NSString *ext = [_importUrl.path pathExtension];
NSString *newName;
for (int i = 2; i < 100000; i++) {
for (int i = 2; i < 100000; i++)
{
newName = [[NSString stringWithFormat:@"%@_%d", [[_importUrl.path lastPathComponent] stringByDeletingPathExtension], i] stringByAppendingPathExtension:ext];
if (![fileMan fileExistsAtPath:[_importGpxPath stringByAppendingPathComponent:newName]])
break;
Expand Down Expand Up @@ -302,14 +289,14 @@ - (void) processUrl:(NSURL *)url showAlerts:(BOOL)showAlerts openGpxView:(BOOL)o
return;

// Try to import gpx
OASKFile *file = [[OASKFile alloc] initWithFilePath:_importUrl.path];
__block OASKFile *file = [[OASKFile alloc] initWithFilePath:_importUrl.path];
_doc = [OASGpxUtilities.shared loadGpxFileFile:file];
if (_doc)
{
// _2024-07-30_.gpx
NSString *fileName = [_importUrl.path lastPathComponent];
// 123/_2024-07-30_.gpx
NSString *importDestFilepath = [_importGpxRelativePath stringByAppendingPathComponent:fileName];
NSString *importDestFilepath = [_importGpxPath stringByAppendingPathComponent:fileName];
if ([[OAGPXDatabase sharedDb] containsGPXItem:importDestFilepath])
{
if (showAlerts)
Expand Down Expand Up @@ -345,6 +332,7 @@ - (void) processUrl:(NSURL *)url showAlerts:(BOOL)showAlerts openGpxView:(BOOL)o
{
dispatch_async(dispatch_get_main_queue(), ^{
[self doPush];
file = item.file;
auto trackItem = [[OASTrackItem alloc] initWithFile:file];
trackItem.dataItem = item;
[[OARootViewController instance].mapPanel openTargetViewWithGPX:trackItem];
Expand All @@ -353,12 +341,12 @@ - (void) processUrl:(NSURL *)url showAlerts:(BOOL)showAlerts openGpxView:(BOOL)o
}

- (void)prepareProcessUrl:(NSURL *)url showAlerts:(BOOL)showAlerts openGpxView:(BOOL)openGpxView completion:(void (^)(BOOL success))completion {
if ([url isFileURL]) {
if ([url isFileURL])
{
[self prepareProcessUrl:^{
[self processUrl:url showAlerts:showAlerts openGpxView:openGpxView];
if (completion) {
if (completion)
completion(YES);
}
}];
}
}
Expand All @@ -375,29 +363,16 @@ - (void)prepareProcessUrl:(void (^)(void))processUrl

- (OASGpxDataItem *)doImport
{
OASGpxDataItem *item;
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:_importGpxPath])
[fileManager createDirectoryAtPath:_importGpxPath withIntermediateDirectories:YES attributes:nil error:nil];
if (_newGpxName)
{
[fileManager copyItemAtPath:_importUrl.path toPath:[_importGpxPath stringByAppendingPathComponent:_newGpxName] error:nil];
}
else
{
[fileManager copyItemAtPath:_importUrl.path
toPath:[_importGpxPath stringByAppendingPathComponent:[self getCorrectedFilename:[_importUrl.path lastPathComponent]]]
error:nil];
}

if (_newGpxName)
{
item = [[OAGPXDatabase sharedDb] addGPXFileToDBIfNeeded:[_importGpxPath stringByAppendingPathComponent:[self getCorrectedFilename:[_importUrl.path lastPathComponent]]]];
}
else
{
[[OAGPXDatabase sharedDb] addGPXFileToDBIfNeeded:[_importGpxPath stringByAppendingPathComponent:[self getCorrectedFilename:[_importUrl.path lastPathComponent]]]];
}
NSString *gpxPath = _newGpxName
? [_importGpxPath stringByAppendingPathComponent:_newGpxName]
: [_importGpxPath stringByAppendingPathComponent:[self getCorrectedFilename:[_importUrl.path lastPathComponent]]];
[fileManager copyItemAtPath:_importUrl.path
toPath:gpxPath
error:nil];
OASGpxDataItem *item = [[OAGPXDatabase sharedDb] addGPXFileToDBIfNeeded:gpxPath];
if (item.color != 0)
[[OAGPXAppearanceCollection sharedInstance] getColorItemWithValue:item.color];

Expand All @@ -424,17 +399,14 @@ - (NSString *)getCorrectedFilename:(NSString *)filename
- (void) removeFromDB
{
NSString *gpxFilePath = [_importUrl.path hasPrefix:_app.gpxPath]
? [OAUtilities getGpxShortPath:_importUrl.path]
: [_importUrl.path lastPathComponent];
? [OAUtilities getGpxShortPath:_importUrl.path]
: [_importUrl.path lastPathComponent];

OASGpxDataItem *item = [[OAGPXDatabase sharedDb] getGPXItemByFileName:gpxFilePath];
if (item)
{
[[OAGPXDatabase sharedDb] removeGpxItem:item withLocalRemove:YES];
} else
{
else
NSLog(@"[OAGPXImportUIHelper] -> [ERROR] -> removeFromDB");
}
}

#pragma mark - OAGPXImportDelegate
Expand Down

0 comments on commit 307bf8d

Please sign in to comment.